aggregation-framework

MongoDB match array based on document value [duplicate]

半腔热情 提交于 2021-01-29 13:16:45
问题 This question already has answers here : Retrieve only the queried element in an object array in MongoDB collection (14 answers) MongoDb query condition on comparing 2 fields (4 answers) Closed 10 months ago . Let's say I have the following document structure: { "A": { "_id": "ID0" }, "B": [ "a": { "_id": "ID0", "field": "X" }, "b": { "_id": "ID1", "field": "Y" } ] } I want to project B matched with the _id in A . The end result would be: { "B": [ "a": { "_id": "ID0", "field": "X" } ] } I

merge multiple documents into one document with both document fields in MongoDB

此生再无相见时 提交于 2021-01-29 08:48:37
问题 I have multiple documents In MobgoDB How to do to the group on "abc" and "xyz" and get one document. Please see the "Output Document". need to do the union with ( Document 1 U Document 2 ) and (Document 1 U Document 3) . U= Union Document 1 { "data": { "Inside_data": { "project": { "abc": { "alpha": 4, "beta" : 45 }, "xyz": { "alpha": 214, "beta" : 431 } } } } } Document 2 "Deal": { "name": "abc", "url" : "www.abc.com, "email": [ "abc@gmail.com"], "total": 2 } Document 3 "Deal": { "name":

how to replace multiple occurrences of a phrase mongodb

假如想象 提交于 2021-01-28 23:33:13
问题 I am trying to replace multiple occurrences of the same phrase in the same field. Here is an example of the field 'imageUrl' contents: "imageUrl" : "https://cdn.test.com/s/files/1/0035/4671/0061/products/Screen_Shot_2019-12-13_at_12.47.01_pm_2000x.png?v=1576212956\n \n https://cdn.test.com/s/files/1/0035/4671/0061/products/Screen_Shot_2019-12-13_at_12.47.01_pm_2000x.png?v=1576212956\n \n \n" My current formula: { $match: { imageUrl: { $regex: "_2000x" }, brand: "Goat The Label" } }, {

how to replace multiple occurrences of a phrase mongodb

房东的猫 提交于 2021-01-28 23:29:52
问题 I am trying to replace multiple occurrences of the same phrase in the same field. Here is an example of the field 'imageUrl' contents: "imageUrl" : "https://cdn.test.com/s/files/1/0035/4671/0061/products/Screen_Shot_2019-12-13_at_12.47.01_pm_2000x.png?v=1576212956\n \n https://cdn.test.com/s/files/1/0035/4671/0061/products/Screen_Shot_2019-12-13_at_12.47.01_pm_2000x.png?v=1576212956\n \n \n" My current formula: { $match: { imageUrl: { $regex: "_2000x" }, brand: "Goat The Label" } }, {

Sorting records in a way so that only records which matches an id comes first?

大城市里の小女人 提交于 2021-01-28 22:05:02
问题 I have a query which return me the records or messages for a certain scenario: const [messages, messageCount] = await Promise.all([ MessageModel.find(params).select(filterObject).limit(ctx.query.limit).skip(ctx.paginate.skip) .sort('-sex -age') .lean(), MessageModel.countDocuments(params), ]); Is there any way to get the records first which matches an object id, and then rest of the records? Result { _id:abc0aa8573bfa917b152cdbc isPrivate:false message:"My name is stark" gender:"unisex" age:

Sorting records in a way so that only records which matches an id comes first?

雨燕双飞 提交于 2021-01-28 22:03:33
问题 I have a query which return me the records or messages for a certain scenario: const [messages, messageCount] = await Promise.all([ MessageModel.find(params).select(filterObject).limit(ctx.query.limit).skip(ctx.paginate.skip) .sort('-sex -age') .lean(), MessageModel.countDocuments(params), ]); Is there any way to get the records first which matches an object id, and then rest of the records? Result { _id:abc0aa8573bfa917b152cdbc isPrivate:false message:"My name is stark" gender:"unisex" age:

How to calculate the percentage using facet in MongoDB?

瘦欲@ 提交于 2021-01-28 21:31:31
问题 I am calculating the notification percentage in my app for tracking some statistics. My Collection: [ { _id: "123", status: "seen", userId: "589" }, { _id: "223", status: "seen", userId: "589" }, { _id: "474", status: "unseen", userId: "589" }, { _id: "875", status: "seen", userId: "112" }, { _id: "891", status: "unseen", userId: "112" } ] Expected Result: Here we can see that, UserId - 589 has received 3 notifications out of which 2 are seen. So the calculation is (totalNumOfSeen

MongoDB $group (mongo playground)

[亡魂溺海] 提交于 2021-01-28 19:36:36
问题 I have asked a question here about the $unwind operator, but am facing issues with grouping the data properly after. I have a mongo playground with an example, but here it is also. After an $unwind , $lookup and $group in the query (maybe there is a better, more efficient way to do it?), I am left with this data: [ { "ExerciseDetail": [ [{ "Name": "Squat", "_id": "5f60c3b7f93d8e00a1cdf414" }], [{ "Name": "Deadlift", "_id": "5f60c3b7f93d8e00a1cdf415" }] ], "Sets": [ { "ExerciseId":

Mongodb: how to “flatten” some query results

怎甘沉沦 提交于 2021-01-28 18:12:06
问题 Help to "flatten" (to pull nested fields at same level as document's fields) a mongodb document in a query //this is "anagrafiche" collection [{ "name": "tizio" ,"surname": "semproni" ,"birthday": "01/02/1923" ,"home": { "road": "via" ,"roadname": "bianca" ,"roadN": 12 ,"city": "rome" ,"country": "italy" } }, { "name": "caio" ,"surname": "giulio" ,"birthday": "02/03/1932" ,"home": { "road": "via" ,"roadname": "rossa" ,"roadN": 21 ,"city": "milan" ,"country": "italy" } }, { "name": "mario" ,

Mongodb: how to “flatten” some query results

旧时模样 提交于 2021-01-28 18:04:46
问题 Help to "flatten" (to pull nested fields at same level as document's fields) a mongodb document in a query //this is "anagrafiche" collection [{ "name": "tizio" ,"surname": "semproni" ,"birthday": "01/02/1923" ,"home": { "road": "via" ,"roadname": "bianca" ,"roadN": 12 ,"city": "rome" ,"country": "italy" } }, { "name": "caio" ,"surname": "giulio" ,"birthday": "02/03/1932" ,"home": { "road": "via" ,"roadname": "rossa" ,"roadN": 21 ,"city": "milan" ,"country": "italy" } }, { "name": "mario" ,