mongodb-query

Get distinct values from array based on conditions within the array

Deadly 提交于 2021-01-27 21:41:25
问题 I have a collection with documents like this: { "_id" : ObjectId("59df91dae4b03289d79efb4e"), "name" : "TEST", "payload" : [ { "_id" : "ABC", "status": "FALSE" }, { "_id":"DEF", "status": "TRUE" }, { "_id" : "XYZ", "status": "NULL" } ] } I am trying to find all the payload._id which have status as TRUE/FALSE. Expected result is ["ABC","DEF"] So far, I have something like this. db.collection.distinct('payload._id',{"_id" : "TEST"}) Not sure how to add conditions for elements within array. 回答1:

MongoDB aggregation to group dynamic keys

不打扰是莪最后的温柔 提交于 2021-01-27 20:06:36
问题 I'm working on an aggregation of counts of the value of a variable number of bar rows like in this data structure : > db.data.find() { "_id" : "foo1", "1" : { "bar" : 6 }, "0" : { "bar" : 11 }, "3" : { "bar" : 8 }, "2" : { "bar" : 0 }, "5" : { "bar" : 8 }, "4" : { "bar" : 19 }, "6" : { "bar" : 8 } } { "_id" : "foo2", "1" : { "bar" : 18 }, "0" : { "bar" : 3 }, "3" : { "bar" : 19 }, "2" : { "bar" : 0 }, "5" : { "bar" : 13 }, "4" : { "bar" : 17 }, "7" : { "bar" : 8 }, "6" : { "bar" : 8 }, "8" :

mongodb query update select nested fields

蓝咒 提交于 2021-01-27 19:21:55
问题 this is my document in mongo: "calendar": { "_id": "5cd26a886458720f7a66a3b8", "hotel": "5cd02fe495be1a4f48150447", "calendar": [ { "_id": "5cd26a886458720f7a66a413", "date": "1970-01-01T00:00:00.001Z", "rooms": [ { "_id": "5cd26a886458720f7a66a415", "room": "5cd17d82ca56fe43e24ae5d3", "price": "", "remaining": 0, "reserved": 0 }, { "_id": "5cd26a886458720f7a66a414", "room": "5cd17db6ca56fe43e24ae5d4", "price": "", "remaining": 0, "reserved": 0 } ] }, } I need to update the objects in the

Can I access the positional $ operator in projection of findOneAndUpdate

孤街醉人 提交于 2021-01-27 15:31:51
问题 I have this query that works, but I want for the doc to only display network.stations.$ instead of the entire array. If I write fields: network.stations.$ , I get an error. Is there a way for the doc only to return a single element from [stations] ? Network.findOneAndUpdate({ "network.stations.id": req.params.station_Id }, { "network.stations.$.free_bikes": req.body.free_bikes }, { new: true, fields: "network.stations" }, (err, doc) => console.log(doc)) // I want doc to somehow point only to

MongoDB Indexing: Multiple single-field vs single compound?

℡╲_俬逩灬. 提交于 2021-01-27 13:23:09
问题 I have a collection of geospatial+temporal data with a few additional properties, which I'll be displaying on a map. The collection has a few million documents at this point, and will grow over time. Each document has the following fields: Location: [geojson object] Date: [Date object] ZoomLevel: [int32] EntryType: [ObjectID] I need to be able to rapidly query this collection by any combination of location (generally a geowithin query), Date (generally $gte/$lt), ZoomLevel and EntryType. What

How to return all fields without specify after $group stage

橙三吉。 提交于 2021-01-27 10:47:35
问题 How to sort the child array in a document and keep the parent fields? I have a collection like this: { "_id" : ObjectId("5d1757929831633ff8abb638"), "name" : "Carraro Exlusive", "description" : "Carraro Exlusive description", "image" : "image-15", "__v" : 0, "sellers" : [ { "_id" : ObjectId("5d1757929831633ff8abb639"), "product" : ObjectId("5d1757929831633ff8abb638"), "seller" : ObjectId("5d1518582f9254189ca92e59"), "price" : 13, "stock" : 5, "__v" : 0 }, { "_id" : ObjectId(

$group inner array values without $unwind

↘锁芯ラ 提交于 2021-01-27 08:11:08
问题 I want to group objects in the array by same value for specified field and produce a count. I have the following mongodb document (non-relevant fields are not present). { arrayField: [ { fieldA: value1, ...otherFields }, { fieldA: value2, ...otherFields }, { fieldA: value2, ...otherFields } ], ...otherFields } The following is what I want. { arrayField: [ { fieldA: value1, ...otherFields }, { fieldA: value2, ...otherFields }, { fieldA: value2, ...otherFields } ], newArrayField: [ { fieldA:

how to count a $lookup fields in mongo db?

最后都变了- 提交于 2021-01-27 04:20:06
问题 I'm quite new in mongodb, now I need to count a $lookup field, is it possible? I had something like this: result = await company.aggregate([ { $lookup: { from: 'userFocus', localField: '_id', foreignField: 'value', as: 'focusUsers' } }, { $project:{ name: 1, focusUsers: {userId: 1} } } ]) and the result looks like this: [ {_id: 'xxxx', name: 'first company', focusUsers: [user1, user2, user3...]}, {_id: 'yyyy', name: 'second company', focusUsers: []}, {_id: 'zzzz', name: 'third company',

Mongodb - Finding GeoNear within nested JSON objects

爱⌒轻易说出口 提交于 2021-01-27 04:12:48
问题 I need to get the closest place near a certain point using this data structure: [ { "data_id": "127", "actual_data": [ { "id": "220", "value": "shaul" }, { "id": "221", "value": "3234324" }, { "id": "222", "value": { "lngalt": [ 13.7572225, -124.0429047 ], "street_number": null, "political": null, "country": null, } }, { "id": "223", "value": "dqqqf1222fs3d7ddd77@Dcc11cS2112s.com" }, { "id": "224", "value": "123123" }, { "id": "225", "value": "lala1" }, .... }, { "data_id": "133", "actual

Mongodb - Finding GeoNear within nested JSON objects

江枫思渺然 提交于 2021-01-27 04:12:33
问题 I need to get the closest place near a certain point using this data structure: [ { "data_id": "127", "actual_data": [ { "id": "220", "value": "shaul" }, { "id": "221", "value": "3234324" }, { "id": "222", "value": { "lngalt": [ 13.7572225, -124.0429047 ], "street_number": null, "political": null, "country": null, } }, { "id": "223", "value": "dqqqf1222fs3d7ddd77@Dcc11cS2112s.com" }, { "id": "224", "value": "123123" }, { "id": "225", "value": "lala1" }, .... }, { "data_id": "133", "actual