aggregation-framework

MongoDB : querying documents with two equal fields, $match and $eq

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 05:12:50
问题 What is the best way to return all documents in a collection if I want document.a == document.b? I've tried db.collection.aggregate([ { $match: { $eq: [ '$a', '$b' ] } }]) But it returns with no errors or results, because I assume it is literally matching strings "$a" and "$b". Is there a different way to specify that these are fields? db.collection.aggregate([ { $project: { eq: { $cond: [ { $eq: [ '$a', '$b' ] }, 1, 0 ] } } }, { $match: { eq: 1 } }]) The above works, but requires the

mongoDB Aggregation: sum based on array names

断了今生、忘了曾经 提交于 2019-12-19 04:29:05
问题 I have the following data of a match: { date: 20140101, duration: 23232, win:[ { player: "Player1", score : 2344324 }, { player: "Player4", score : 23132 } ], loss:[ { player: "Player2", score : 324 }, { player: "Player3", score : 232 } ] } Now i want to count the wins and losses for all players like this: result : [ { player : "Player1", wins : 12, losses : 2 }, { player : "Player2", wins : 7, losses : 8 } ] My problem is that the win/loss information only exists in the name of the array.

Speed up MongoDB aggregation

风流意气都作罢 提交于 2019-12-19 04:09:50
问题 I have a sharded collection "my_collection" with the following structure: { "CREATED_DATE" : ISODate(...), "MESSAGE" : "Test Message", "LOG_TYPE": "EVENT" } The mongoDB environment is sharded with 2 shards. The above collection is sharded using Hashed shard key on LOG_TYPE. There are 7 more other possibilities for LOG_TYPE attribute. I have 1 million documents in "my_collection" and I am trying to find the count of documents based on the LOG_TYPE using the following query: db.my_collection

$project in $lookup mongodb

假装没事ソ 提交于 2019-12-19 03:09:02
问题 I have a query, that use $lookup to "join" two models, after this i use $project to select olny the fields that i need, but my $project brings an arrray of objects ( user_detail ) that contains more data that i need. I want only two fields ( scheduleStart and scheduleEnd ) of my result. My query: User.aggregate([{ $match: { storeKey: req.body.store, } }, { $group: { _id: { id: "$_id", name: "$name", cpf: "$cpf", phone: "$phone", email: "$email", birthday: "$birthday", lastName: "$lastname" },

$project in $lookup mongodb

耗尽温柔 提交于 2019-12-19 03:08:36
问题 I have a query, that use $lookup to "join" two models, after this i use $project to select olny the fields that i need, but my $project brings an arrray of objects ( user_detail ) that contains more data that i need. I want only two fields ( scheduleStart and scheduleEnd ) of my result. My query: User.aggregate([{ $match: { storeKey: req.body.store, } }, { $group: { _id: { id: "$_id", name: "$name", cpf: "$cpf", phone: "$phone", email: "$email", birthday: "$birthday", lastName: "$lastname" },

MongoDB - objects? Why do I need _id in aggregate

主宰稳场 提交于 2019-12-18 20:10:09
问题 Here is an example from MongoDB tutorial (here it collection ZIP Code db: db.zipcodes.aggregate( [ { $group: { _id: "$state", totalPop: { $sum: "$pop" } } }, { $match: { totalPop: { $gte: 10*1000*1000 } } } ] ) if I replace _id with something else like word Test , I will get error message: "errmsg" : "exception: the group aggregate field 'Test' must be defined as an expression inside an object", "code" : 15951, "ok" : 0 Could anybody help me understand why I need _id in my command? I thought

MongoDB - objects? Why do I need _id in aggregate

China☆狼群 提交于 2019-12-18 20:10:02
问题 Here is an example from MongoDB tutorial (here it collection ZIP Code db: db.zipcodes.aggregate( [ { $group: { _id: "$state", totalPop: { $sum: "$pop" } } }, { $match: { totalPop: { $gte: 10*1000*1000 } } } ] ) if I replace _id with something else like word Test , I will get error message: "errmsg" : "exception: the group aggregate field 'Test' must be defined as an expression inside an object", "code" : 15951, "ok" : 0 Could anybody help me understand why I need _id in my command? I thought

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:

How to convert MongoDB aggregation query to Laravel MongoDB by jenssegers

放肆的年华 提交于 2019-12-18 13:31:09
问题 I have MongoDB collection called changes which contains following data { "date" : ISODate("2014-06-09T00:00:00.000Z"), "field" : "ip", "from" : "157.11.209.123", "to" : "107.21.109.254" } { "date" : ISODate("2014-05-15T00:00:00.000Z"), "field" : "ip", "from" : "107.21.109.254", "to" : "157.11.209.123" } { "date" : ISODate("2014-06-09T00:00:00.000Z"), "field" : "registration", "from" : "Old service", "to" : "Some new service" } Then I want to make typical SQL query that counts occurrences and