Get first element in array and return using Aggregate?
Since 3.2, we can use $arrayElemAt to get the first element in an array db.my_collection.aggregate([ { $project: { resp : { $arrayElemAt: [‘$my_field’,0] } }} ])
Since 3.2, we can use $arrayElemAt to get the first element in an array db.my_collection.aggregate([ { $project: { resp : { $arrayElemAt: [‘$my_field’,0] } }} ])
The results returned from the aggregation pipeline are just plain objects. So you do the sorting as a pipeline stage, not as a separate operation: Recommend.aggregate( [ // Grouping pipeline { “$group”: { “_id”: ‘$roomId’, “recommendCount”: { “$sum”: 1 } }}, // Sorting pipeline { “$sort”: { “recommendCount”: -1 } }, // Optionally limit results … Read more
If you are wanting to do a pattern match on numbers, the way to do it in mongo is use the $where expression and pass in a pattern match. > db.test.find({ $where: “/^123.*/.test(this.example)” }) { “_id” : ObjectId(“4bfc3187fec861325f34b132”), “example” : 1234 }
Suppose the bottom left coordinates and the upper right coordinates are respectively [0, 0] and [100, 100]. From MongoDB 3.2 you can use the $slice operator to return a subset of an array which is what you want. db.collection.aggregate([ { “$match”: { “loc”: { “$geoWithin”: { “$box”: [ [0, 0], [100, 100] ] } }} … Read more
Sure it is, just clearing up the syntax but you basically had it: { “opened”: { “$elemMatch”: { “closed”: false, “$or”: [ { “openingEvening”: { “$lte”: currentTime }, “closingEvening”: { “$gte”: currentTime } }, { “openingMorning”: { “$lte”: currentTime }, “closingMorning”: { “$gte”: currentTime } } ] } } } And given a sample idea … Read more
Using the example in the manual on the new bulkWrite() API, consider the following test collection which contains the following documents: { “_id” : 1, “char” : “Brisbane”, “class” : “monk”, “lvl” : 4 }, { “_id” : 2, “char” : “Eldon”, “class” : “alchemist”, “lvl” : 3 }, { “_id” : 3, “char” : … Read more
Not in “mongoose” specifically, or at least not yet as of writing. The MongoDB shell as of the 2.6 release actually uses the “Bulk operations API” “under the hood” as it were for all of the general helper methods. In it’s implementation, it tries to do this first, and if an older version server is … Read more
Nothing wrong with what you are basically attempting, but perhaps the only clarification here is the common misconception that you need operators like $nin or $in when querying an array. Also you really need to do here is a basic inequality match with $ne: Person.find({ “groups”: { “$ne”: group._id } }) The “array” operators are … Read more
Add Stage 2.5 😛 { $project:{ FileName:1, FileSize:{$substr:[“$FileSize”, 0, -1 ]} } } FileSize is a integer and there is no operation to convert it to String. So you can use hack, and use substr to convert it to string, 0 for starting and -1 rest of the string.
What you need is the $cond operator of aggregation framework. One way to get what you want would be: db.foo.aggregate([ { $project: { item: 1, lessThan10: { // Set to 1 if value < 10 $cond: [ { $lt: [“$value”, 10 ] }, 1, 0] }, moreThan10: { // Set to 1 if value > … Read more