aggregation-framework

MongoDB: Is it possible to limit the results of $lookup to certain fields (as a projection)?

瘦欲@ 提交于 2019-12-24 07:39:22
问题 Here is some code, guys. db.collection('bugs').aggregate([{ $match: finder }, { $sort: { name: 1 } }, { $limit: startrecord + settings.pagination_limit }, { $skip: startrecord }, { $lookup: { from: 'users', localField: 'user', foreignField: '_id', as: 'user' } }], { collation: collation }, function(err, docs) { It works perfectly, it's a plain lookup. However I only need a few fields from the collection "users", and the $lookup returns everything. Is there a way to apply a projection to the

MongoDB: How to “union all” results from same collection?

╄→гoц情女王★ 提交于 2019-12-24 07:27:22
问题 query.1: db.test.find({category_id:1}).sort({createAt:-1}).limit(5); query.2: db.test.find({category_id:2}).sort({createAt:-1}).limit(5); What I want is use one query get results of query1+query2, and then sort the results by createAt . 回答1: You can use $facet aggregation here. db.test.aggregate([ { "$facet": { "first": [ { "$match": { "category_id": 1 }}, { "$sort": { "createAt": -1 }}, { "$limit": 5 } ], "second": [ { "$match": { "category_id": 2 }}, { "$sort": { "createAt": -1 }}, { "

Compare and add two array inside nested arrays

流过昼夜 提交于 2019-12-24 07:26:00
问题 I have below array of array inside my document { items: [ ["Tax", 10, 20, 30], ["FX Adjustments", 10, 20, 30], ["Tax", 10, 20, 30], ["FX Adjustments", 10, 20, 30] ] } I need to combine Tax with Tax and FX Adjustments with FX Adjustments . Output I need { items: [ ["Tax", 20, 40, 60], ["FX Adjustments", 20, 40, 60] ] } I tried but with no luck db.collection.aggregate([ { $project: { items: { $reduce: { input: "$items", initialValue: [], in: { $cond: [ ] } } } } } ]) Kindly help with this Thank

MongoDB to assist with recommendations

亡梦爱人 提交于 2019-12-24 06:12:39
问题 I have a 3 collection schema as shown below: User collection has information regarding their friends and the listening count(weight) per artist { user_id : 1, Friends : [3,5,6], Artists : [ {artist_id: 10 , weight : 345}, {artist_id: 17 , weight : 378} ] } Artist collection schema has information regarding the name of the artist, the tags given by various users to them. { artistID : 56, name : "Ed Sheeran", user_tag : [ {user_id : 2, tag_id : 6}, {user_id : 2, tag_id : 5}, {user_id : 3, tag

MongoDB to assist with recommendations

旧时模样 提交于 2019-12-24 06:12:17
问题 I have a 3 collection schema as shown below: User collection has information regarding their friends and the listening count(weight) per artist { user_id : 1, Friends : [3,5,6], Artists : [ {artist_id: 10 , weight : 345}, {artist_id: 17 , weight : 378} ] } Artist collection schema has information regarding the name of the artist, the tags given by various users to them. { artistID : 56, name : "Ed Sheeran", user_tag : [ {user_id : 2, tag_id : 6}, {user_id : 2, tag_id : 5}, {user_id : 3, tag

Is it possible to switch the dividend and the divisor of MongoDB's $mod query operator?

孤街浪徒 提交于 2019-12-24 05:48:07
问题 MongoDB's $mod query operator provides a way to select documents where the value V of a field divided by a divisor D has the specified remainder R . So it selects documents where V mod D equals R . I need to switch the dividend and the divisor of the modulo operation. In other words, I need to select documents where a provided dividend D divided by the value V of a field has the specified remainder R . So I need to select documents where D mod V equals R . Is it possible to switch the

Mongoose populate either ObjectId reference or String

别来无恙 提交于 2019-12-24 05:36:04
问题 Is there a way to specify a heterogeneous array as a schema property where it can contain both ObjectIds and strings? I'd like to have something like the following: var GameSchema = new mongoose.schema({ players: { type: [<UserModel reference|IP address/socket ID/what have you>] } Is the only option a Mixed type that I manage myself? I've run across discriminators, which look somewhat promising, but it looks like it only works for subdocuments and not references to other schemas. Of course, I

Mongoose populate either ObjectId reference or String

我的梦境 提交于 2019-12-24 05:36:03
问题 Is there a way to specify a heterogeneous array as a schema property where it can contain both ObjectIds and strings? I'd like to have something like the following: var GameSchema = new mongoose.schema({ players: { type: [<UserModel reference|IP address/socket ID/what have you>] } Is the only option a Mixed type that I manage myself? I've run across discriminators, which look somewhat promising, but it looks like it only works for subdocuments and not references to other schemas. Of course, I

Mongo - Match where object key is variable

筅森魡賤 提交于 2019-12-24 04:49:08
问题 I have a Mongo DB with the following object: [ { "link" : "xxxxx.jpg" "_id" : ObjectId("5501b1648ef0b4eccc41814e"), "processed" : { "320" : true, "480" : true, "540" : true, "720" : true, "800" : true, "1080" : true, "original" : false, "iPhone" : true } } ] I am trying to query where any of the processed values is false, but I cannot seem to figure out how to query where I do not know which key matches. Is this possible without looping through all documents? 回答1: With a document schema like

Group by hour and count MongoDB

本小妞迷上赌 提交于 2019-12-24 04:32:34
问题 I'm on a project working with data of a bike-sharing service. Each trip has the following info > db.bikes.find({bike:9990}).pretty() { "_id" : ObjectId("5bb59fd8e9fb374bf0cd5c1c"), "gender" : "M", "userAge" : 49, "bike" : 9990, "depStation" : 150, "depDate" : "01/08/2018", "depHour" : "0:00:13", "arrvStation" : 179, "arrvDate" : "01/08/2018", "arrvHour" : "0:23:38" } How do I group for each hour of the day and count the number of trips made in that specific hour? I'm trying with this query db