mongodb-query

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

How to find a subfield in Mongo without knowing the parent field?

我们两清 提交于 2019-12-24 06:29:48
问题 Given the following collection: db.test.insertMany([ {"_id": 1, "Jimmy": {"Loved by mom": true}}, {"_id": 2, "Andrew": {"Loved by mom": false}}, {"_id": 3, "Nicholas": {"Loved by mom": true}}", {"_id": 4, "Sarah": {"Loved by dad": true}} ]); Is there a way to search for all documents that have the subfield "Loved by mom" , without knowing what the parent field is called? To (hopefully) simplify this task, the desired subfield is always located at depth 1. 回答1: Is there a way to search for all

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