mongodb-query

Reverse populate in mongoose

孤者浪人 提交于 2020-08-27 21:27:04
问题 How can i reverse populate in mongo. I have 2 schema's User: var user_scheme = new mongoose.Schema({ name:String, age:Number, roles:{ type:mongoose.Schema.Types.ObjectId, ref:'Role' } }); Role: var role_scheme = new mongoose.Schema({ name:String, }); Documents: ///user { "_id" : ObjectId("5bf9a19b01ce3e19b440aed8"), "name" : "user1", "age" : 22, "roles" : ObjectId("5c0242621ab7b677e6b2e01e"), "__v" : 0 } ///role { "_id" : ObjectId("5c0242621ab7b677e6b2e01e"), "name" : "Admin" } Code: User

$lookup when foreignField is in nested array

假装没事ソ 提交于 2020-08-27 06:43:08
问题 I have two collections : Student { _id: ObjectId("657..."), name:'abc' }, { _id: ObjectId("593..."), name:'xyz' } Library { _id: ObjectId("987..."), book_name:'book1', issued_to: [ { student: ObjectId("657...") }, { student: ObjectId("658...") } ] }, { _id: ObjectId("898..."), book_name:'book2', issued_to: [ { student: ObjectId("593...") }, { student: ObjectId("594...") } ] } I want to make a Join to Student collection that exists in issued_to array of object field in Library collection. I

mongodb join multiple collections [duplicate]

Deadly 提交于 2020-08-25 07:14:11
问题 This question already has answers here : Multiple join conditions using the $lookup operator (4 answers) Closed 2 years ago . I'd like to "join" 3 Collections in MongoDB by using mongochef. The collections are "Order", "Employee" and "City". I tried to use temp collections, but it is not effective. Now I am using var = a for the first "join". If I 'd like to show "a", there are displayed 20 results only. Do you have an idea or another solution? var a = db.Order.aggregate([ { $lookup: { from:

mongodb join multiple collections [duplicate]

只愿长相守 提交于 2020-08-25 07:14:00
问题 This question already has answers here : Multiple join conditions using the $lookup operator (4 answers) Closed 2 years ago . I'd like to "join" 3 Collections in MongoDB by using mongochef. The collections are "Order", "Employee" and "City". I tried to use temp collections, but it is not effective. Now I am using var = a for the first "join". If I 'd like to show "a", there are displayed 20 results only. Do you have an idea or another solution? var a = db.Order.aggregate([ { $lookup: { from:

$setIsSubset for regular queries in Mongo

时光总嘲笑我的痴心妄想 提交于 2020-08-25 07:11:51
问题 I am looking to do the equivalent of $setIsSubset http://docs.mongodb.org/manual/reference/operator/aggregation/setIsSubset/ for regular (i.e. NOT aggregate) queries in MongoDB. How can I do this? Assume that I have the documents { 'x' : ['A', 'B'] } { 'x' : ['A', 'D'] } And that filter = ['A', 'B', C'] I want to do a find({"x" : {'$setIsSubSet':filter}}) and expect only to get back { 'x' : ['A', 'B'] } It seems like most conditional commands match any not all. I also want it to be a subset,

$setIsSubset for regular queries in Mongo

岁酱吖の 提交于 2020-08-25 07:11:39
问题 I am looking to do the equivalent of $setIsSubset http://docs.mongodb.org/manual/reference/operator/aggregation/setIsSubset/ for regular (i.e. NOT aggregate) queries in MongoDB. How can I do this? Assume that I have the documents { 'x' : ['A', 'B'] } { 'x' : ['A', 'D'] } And that filter = ['A', 'B', C'] I want to do a find({"x" : {'$setIsSubSet':filter}}) and expect only to get back { 'x' : ['A', 'B'] } It seems like most conditional commands match any not all. I also want it to be a subset,

MongoDB — Find duplicate documents by multiple keys

∥☆過路亽.° 提交于 2020-08-25 04:48:11
问题 I have a collection with documents that look like the following: { "_id" : ObjectId("55b377cb66b393427367c3e2"), "comment" : "This is a comment", "url_key" : "55b377cb66b393427367c3df", //This is an ObjectId from another record in a different collection } I need to find records in this collection that contain duplicate values for the both the comment AND the url_key. I can easily generate (using aggregate) duplicate records for the same, single, key (eg: comment), but I can't figure out how

MongoDB — Find duplicate documents by multiple keys

浪子不回头ぞ 提交于 2020-08-25 04:48:01
问题 I have a collection with documents that look like the following: { "_id" : ObjectId("55b377cb66b393427367c3e2"), "comment" : "This is a comment", "url_key" : "55b377cb66b393427367c3df", //This is an ObjectId from another record in a different collection } I need to find records in this collection that contain duplicate values for the both the comment AND the url_key. I can easily generate (using aggregate) duplicate records for the same, single, key (eg: comment), but I can't figure out how

Random sort order

梦想的初衷 提交于 2020-08-24 05:31:13
问题 The question about the way to get a random document from collection has been asked many times and there were suggestions on this topic. What I need is to get several random documents from collection and what is even worse - those documents must match certain criterias (filtered, I mean). For example, I have a collection of articles where each article has a 'topic' field. The User chooses a topic he's interested in, and my db must show the corresponding articles each time in random order.

Print document value in mongodb shell

心不动则不痛 提交于 2020-08-22 12:37:51
问题 I want to print the value for this JSON document in the mongo shell. Like a simple console output, without creating a new collection or document. Thanks in advance 回答1: I found a solution, by using .forEach() to apply a JavaScript method: db.widget.find( { id : "4" }, {quality_level: 1, _id:0} ).forEach(function(x) { print(x.quality_level); }); 回答2: db.collection.find() returns a cursor . There are a bunch of methods for the cursor object which can be used to get the cursor information,