aggregation-framework

Sort Documents Without Existing Field to End of Results

∥☆過路亽.° 提交于 2019-12-18 06:58:50
问题 I have the following documents, few documents only have bids fields. Collection: { "_id" : "PqwSsLb2jsqTycMWR", "name" : "aaa", "bids" : [ { "amount" : NumberInt(450) } ] } { "_id" : "93EDoQfeYEFk8pyzX", "name" : "bbb" } { "_id" : "j5wkK5Eagnwuo8Jym", "name" : "ccc", "bids" : [ { "amount" : NumberInt(520) } ] } { "_id" : "eLaTyM5h5kqA97WQQ", "name" : "ddd" } If I sort with bids.amount : 1 am getting below results Result: { "_id" : "93EDoQfeYEFk8pyzX", "name" : "bbb" } { "_id" :

Mongo average aggregation query with no group

爷,独闯天下 提交于 2019-12-18 05:45:26
问题 I am trying to get the average of a whole field using the aggregation framework in Mongo. However i can't seem to find any example that uses it without a group parameter. I have the following document structure: { "_id" : ObjectId("5352703b61d2739b2ea44e4d"), "Semana" : "2014-02-23 - 2014-03-01", "bolsaDeValores" : "7", "bvc" : "8", "dollar" : "76", "ecopetrol" : "51", "dollarPrice" : "18" } Basically what i want to do is get the average value of the bvc field, and any other numeric one, for

Recursive search on a collection in MongoDB

余生长醉 提交于 2019-12-18 05:42:15
问题 I have a list of documents in MongoDB with tree structure, where Model Tree Structures with Parent References pattern used. I want a single aggregation query which returns ancestor list(till the root), given the 'name' property. Structure: { '_id': '1', 'name': 'A', 'parent': '', }, { '_id': '2', 'name': 'B', 'parent': 'A', }, { '_id': '3', 'name': 'C', 'parent': 'B', }, { '_id': '4', 'name': 'D', 'parent': 'C', } Aggregation result:(Given, name = 'D') { '_id': '4', 'name': 'D', 'ancestors':

Use $gte and &lte mongo operator if date is in string format in mongodb

℡╲_俬逩灬. 提交于 2019-12-18 03:47:18
问题 I have dob field like {dob:"02-23-2000"} in database. Now I want to perform $gte and &lte on dob field that is in string format like below: db.panelists.count({"dob":{ '$gte': '08-02-1998', '$lte': '08-02-2003' }}); I am getting count value as zero always. Could anyone please help me out to resolve this query in same dob format. 回答1: You can try below aggregation in mongodb 3.6 You can use $dateFromString aggregation to convert your dob from string to date and then easily $count the documents

MongoDB Aggregation unwind more than one array

家住魔仙堡 提交于 2019-12-18 03:42:01
问题 This is what my documents look like { code: "A2df", clicks: 7, countries: [{"country":"IN", clicks:5},{"country":"US", clicks:2}], domains: [{"domain":"a.com", clicks:4},{"country":"b.com", clicks:3}] }, { code: "B3ws", clicks: 11, countries: [{"country":"IN", clicks:6},{"country":"ND", clicks:5}], domains: [{"domain":"a.com", clicks:7},{"country":"c.com", clicks:4}] }, { code: "A2df", clicks: 5, countries: [{"country":"IN", clicks:2},{"country":"ND", clicks:3}], domains: [{"domain":"a.com",

Obtaining $group result with group count

喜你入骨 提交于 2019-12-17 23:33:48
问题 Assuming I have a collection called "posts" (in reality it is a more complex collection, posts is too simple) with the following structure: > db.posts.find() { "_id" : ObjectId("50ad8d451d41c8fc58000003"), "title" : "Lorem ipsum", "author" : "John Doe", "content" : "This is the content", "tags" : [ "SOME", "RANDOM", "TAGS" ] } I expect this collection to span hundreds of thousands, perhaps millions, that I need to query for posts by tags and group the results by tag and display the results

Mongoose sort the aggregated result

…衆ロ難τιáo~ 提交于 2019-12-17 22:59:22
问题 I'm having a lot of difficulty in solving this mongodb (mongoose) problem. There is this schema 'Recommend' (username, roomId, ll and date) and its collection contains recommendation of user. I need to get a list of most recommended rooms (by roomId). Below is the schema and my tried solution with mongoose query. var recommendSchema = mongoose.Schema({ username: String, roomId: String, ll: { type: { type: String }, coordinates: [ ] }, date: Date }) recommendSchema.index({ ll: '2dsphere' });

MongoDB elemmatch multiple elements in array

邮差的信 提交于 2019-12-17 22:52:16
问题 I have an mongodb document like { "_id" : ObjectId("54e66b2da7b5f3a92e09dc6c"), "SomeMetric" : [ { //some object } { //some object } ], "FilterMetric" : [ { "min" : "0.00", "max" : "16.83", "avg" : "0.00", "class" : "s1" }, { "min" : "0.00", "max" : "16.83", "avg" : "0.00", "class" : "s2" }, { "min" : "0.00", "max" : "16.83", "avg" : "0.00", "class" : "s1" }, { "min" : "0.00", "max" : "16.83", "avg" : "0.00", "class" : "s2" } ] } Typically it contains many nested arrays like this. I want to

$addFields when no $match found

好久不见. 提交于 2019-12-17 21:12:24
问题 Iam new Mongodb developer i wrote mongodb aggregation.one of my field lampStatus : "OFF" is 30 recors are there iam using "{ $match: {lampStatus : "OFF"}}"i got 30 records but ' lampStatus : "OFF" 'there is no records iam getting Fetched 0 record(s) but how to get zero value in the above aggergation. db.collection.aggregate([ { $match:{'type':'L'}}, { $match: {lampStatus : "ON"}}, { $group: { _id : null, TotalLights: { $sum: 1 } } }, { $project: { _id: 0, TotalLights: 1 } } ]) output:Fetched

MongoDB: $lookup returns empty array [duplicate]

拜拜、爱过 提交于 2019-12-17 20:26:06
问题 This question already has answers here : Mongoose always returning an empty array NodeJS (1 answer) Querying after populate in Mongoose (6 answers) Closed last year . I have two models, user and schedule, and i want to combine these two using $lookup and mongoose. User (model) name: { type: String, required: true }, firstName: { String }, lastName: { String }, storeKey: { type: String, required: true }, avatar: String, birthday: String, phone: { type: String }, doc: String, email: { type: