mongodb-query

Finding two documents in MongoDB that share a key value

不问归期 提交于 2019-12-18 17:07:12
问题 I have a large collection of documents in MongoDB, each one of those documents has a key called "name", and another key called "type". I would like to find two documents with the same name and different types , a simple MongoDB counterpart of SELECT ... FROM table AS t1, table AS t2 WHERE t1.name = t2.name AND t1.type <> t2.type I can imagine that one can do this using aggregation: however, the collection is very large, processing it will take time and I'm looking just for one pair of such

Mongoose Populate after Aggregate

时光总嘲笑我的痴心妄想 提交于 2019-12-18 16:59:20
问题 I am trying to get a specific data model after I run an aggregate pipeline followed by populate but I am falling just short of it. The desired result in the end is the following: [ { _accountId: "5beee0966d17bc42501f1234", name: "Company Name 1", contactEmail: "email1@email.com", contactName: "contact Name 1" reason: "Warranties", total: 1152, lineItems: [ { _id: "5beee0966d17bc42501f5086", jobsiteAddress: "1234 Street Southwest Sunnyville, Wyoming 12345", warrantyFee: 384 }, { _id:

MongoDB- Insert if it doesn't exist, else skip

倾然丶 夕夏残阳落幕 提交于 2019-12-18 14:15:39
问题 Is it possible to insert in Mongo with condition; //Pseudo code Bulk Insert Item : If Key exists Skip, don't throw error If key does not exist Add item If i do single inserts, it might return an error or insert in the collection, but is it possible in bulk ? 回答1: You have two real choices here depending on how you want to handle things: Use upsert functionality of MongoDB to essentially "lookup" if the key data exists. If not then you only pass in data to $setOnInsert and that will not touch

MongoDB - Aggregation Framework (Total Count)

 ̄綄美尐妖づ 提交于 2019-12-18 13:01:17
问题 When running a normal "find" query on MongoDB I can get the total result count (regardless of limit) by running "count" on the returned cursor. So, even if I limit to result set to 10 (for example) I can still know that the total number of results was 53 (again, for example). If I understand it correctly, the aggregation framework, however, doesn't return a cursor but simply the results. And so, if I used the $limit pipeline operator, how can I know the total number of results regardless of

MongoDB aggreagte fill missing days [duplicate]

蹲街弑〆低调 提交于 2019-12-18 12:19:09
问题 This question already has answers here : Fill missing dates in records (4 answers) Closed 9 months ago . I have a product collection with the following documents: { "_id" : 1, "item" : "abc", created: ISODate("2014-10-01T08:12:00Z") } { "_id" : 2, "item" : "jkl", created: ISODate("2014-10-02T09:13:00Z") } { "_id" : 3, "item" : "hjk", created: ISODate("2014-10-02T09:18:00Z") } { "_id" : 4, "item" : "sdf", created: ISODate("2014-10-07T09:14:00Z") } { "_id" : 5, "item" : "xyz", created: ISODate(

Mongo find duplicates for entries for two or more fields

无人久伴 提交于 2019-12-18 11:53:01
问题 I have documents like this: { "_id" : ObjectId("557eaf444ba222d545c3dffc"), "foreing" : ObjectId("538726124ba2222c0c0248ae"), "value" : "test", } I want to find all documents which have duplicated values for pair foreing & value . 回答1: You can easily identify the duplicates by running the following aggregation pipeline operation: db.collection.aggregate([ { "$group": { "_id": { "foreing": "$foreing", "value": "$value" }, "uniqueIds": { "$addToSet": "$_id" }, "count": { "$sum": 1 } } }, { "

Mongodb find() query : return only unique values (no duplicates) [duplicate]

一笑奈何 提交于 2019-12-18 10:59:13
问题 This question already has answers here : Get distinct records values (5 answers) Closed 4 years ago . I have a collection of documents : { "networkID": "myNetwork1", "pointID": "point001", "param": "param1" } { "networkID": "myNetwork2", "pointID": "point002", "param": "param2" } { "networkID": "myNetwork1", "pointID": "point003", "param": "param3" } ... pointIDs are unique but networkIDs are not. Is it possible to query Mongodb in such a way that the result will be : [myNetwork1,myNetwork2]

How to merge array field in document in Mongo aggregation

蹲街弑〆低调 提交于 2019-12-18 10:45:49
问题 I have one requirement where i need to do aggregation on two records both have an array field with different value. What I need that when I do aggregation on these records the result should have one array with unique values from both different arrays. Here is example : First record { Host:"abc.com" ArtId:"123", tags:[ "tag1", "tag2" ] } Second record { Host:"abc.com" ArtId:"123", tags:[ "tag2", "tag3" ] } After aggregation on host and artid i need result like this: { Host: "abc.com", ArtId:

Return results mongoose in find query to a variable

一个人想着一个人 提交于 2019-12-18 10:19:50
问题 I need to return the results of a query with mongoose in node.js. How do you return the value to set the value to a variable? What I need to do is: var results = users.findOne({_id : users_list[i]['user_id']},{email : 1, credits : 1},{}, function(err, docs) { if( err || !docs) { console.log("No user found"); } else { return docs; }; }); In order to have: results = docs Thanks a lot for your reply . I also have another problem. How to pass variable in a query operator with find or findOne?

Update array with multiple conditions in mongodb

旧巷老猫 提交于 2019-12-18 09:27:07
问题 I have this document: { "_id" : ObjectId("5b673f525ef92ec6ef16504e"), "events" : [ { "name" : "Winner", "map" : 0, "something" : [] }, { "name" : "Winner", "map" : 2, "something" : [] }, { "name" : "DifferentName", "map" : 2, "something" : [] } ] } If I run the following update: db.getCollection('test').updateOne({ "_id": ObjectId("5b673f525ef92ec6ef16504e"), "events.name": "Winner", "events.map": 2 }, {$push: { "events.$.something": { something: "test", } } }) I get the bad result: { "_id" :