mongodb-query

Mongodb - Finding GeoNear within nested JSON objects

醉酒当歌 提交于 2021-01-27 04:11:14
问题 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

Returning both old and new entities from Spring/MongoDB findAndModify

穿精又带淫゛_ 提交于 2021-01-26 07:15:20
问题 We're using MongoDB via Spring Data and rely on the findAndModify operation to update existing entities or create new ones. In the findAndModify we can configure to return old state of the entity or the new one using returnNew(...) . Is there some way to return both old and new entities from findAndModify ? We need to compare entity states before and after update, this is why we need both instances. At the moment we're resorting to requireNew(false) and then manually update a copy of the old

Any way to get the modified IDS from mongodb's bulk operations using mongoose?

点点圈 提交于 2021-01-26 06:06:37
问题 let dbOperations = Edge.collection.initializeOrderedBulkOp() edges.forEach(edge => { dbOperations.find({_id: edge.id}).upsert().updateOne({ $set: { value: edge.value }, $setOnInsert: { _id: edge.id } }) }) dbOperations.execute() .then(result => { console.log(result.nModified) // This shows the number of edges that is actually modified console.log(result.getModifiedIds()) // This is what I want to achieve }) Any way to achieve this? 回答1: Well from one point of view the anwer is "no" and there

Convert multiple array into one array in MongoDB, also remove the None values from Arrays

大城市里の小女人 提交于 2021-01-21 11:32:45
问题 Mongo DB Array Structure: { "time" :[ [2018-12-18T20:16:28.800Z , 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z], [2016-11-14T10:16:32.700Z, 2017-09-17T11:16:54.500Z, 2018-12-18T20:16:28.800Z], [2017-09-17T11:16:54.500Z, 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z] ], "bar": [ [2,5,9,8], [6,3,7,None], [7,8,None] ] } Expected output with One array without None values: "time" :[ 2018-12-18T20:16:28.800Z, 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z, 2016-11-14T10:16:32.700Z,

Convert multiple array into one array in MongoDB, also remove the None values from Arrays

蹲街弑〆低调 提交于 2021-01-21 11:31:08
问题 Mongo DB Array Structure: { "time" :[ [2018-12-18T20:16:28.800Z , 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z], [2016-11-14T10:16:32.700Z, 2017-09-17T11:16:54.500Z, 2018-12-18T20:16:28.800Z], [2017-09-17T11:16:54.500Z, 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z] ], "bar": [ [2,5,9,8], [6,3,7,None], [7,8,None] ] } Expected output with One array without None values: "time" :[ 2018-12-18T20:16:28.800Z, 2018-12-18T20:16:28.800Z, 2016-11-14T10:16:32.700Z, 2016-11-14T10:16:32.700Z,

Inserting data to nested array in mongodb

一笑奈何 提交于 2021-01-12 06:12:43
问题 I have a document which looks like this { _id:1, list_id:23, name:'list01' cards:[ { id:3, name:'card01' categories:[{ id:10, category:'section01', tags:[{id:11,name:'tag01',is_selected: true}] }] } ] } I need to insert/push some data to tags array in a selected category for a given list_id but I'm getting an error saying MongoError: Too many positional (i.e. '$') elements found in path 'cards.$.categories.$.tags' This is the query that I have tried out. What's wrong with this query any idea

MongoDB aggregation $or with $elemMatch, $expr inside $lookup pipeline

橙三吉。 提交于 2021-01-05 11:50:20
问题 I have 2 collections : Users collection : { _id: ObjectId } Toto Collection : { _id: ObjectId fieldA: {_id: ObjectId} fieldB: [{_id: ObjectId}, {_id: ObjectId}] } And I'm trying to join the element of A collection which fieldA's id OR one of fieldB's id is matching with user's id with following aggregation : db.users.aggregate([ { $lookup: { from: "Toto", let: { "user_id": "$_id" }, as: "fcollection", pipeline: [ { $match: {$or:[ {"fieldB": {$elemMatch: {"_id": "$$user_id"}}}, {$expr: {$eq :[

MongoDB aggregation $or with $elemMatch, $expr inside $lookup pipeline

人盡茶涼 提交于 2021-01-05 11:49:25
问题 I have 2 collections : Users collection : { _id: ObjectId } Toto Collection : { _id: ObjectId fieldA: {_id: ObjectId} fieldB: [{_id: ObjectId}, {_id: ObjectId}] } And I'm trying to join the element of A collection which fieldA's id OR one of fieldB's id is matching with user's id with following aggregation : db.users.aggregate([ { $lookup: { from: "Toto", let: { "user_id": "$_id" }, as: "fcollection", pipeline: [ { $match: {$or:[ {"fieldB": {$elemMatch: {"_id": "$$user_id"}}}, {$expr: {$eq :[

MongoDB - Get IDs of inserted and existing documents after “Insert if not exist” operation on multiple documents

爱⌒轻易说出口 提交于 2021-01-05 11:24:08
问题 I have to insert multiple documents if they don't already exist, but the important thing is that in the query results I need to have IDs of both the inserted and already existing items. I'm trying with the following bulkWrite operation: // external_id is a unique id other than the mongo _id let items = [ {external_id: 123, name: "John"}, {external_id: 456, name: "Mike"}, {external_id: 789, name: "Joseph"} ]; db.collection("my_collection") .bulkWrite( items.map((item) => { return { updateOne:

MongoError: Expression $in takes exactly 2 arguments. 1 were passed in

邮差的信 提交于 2021-01-05 06:37:28
问题 below is my query: while executing below query getting mongoerror : Expression $in takes exactly 2 arguments. 1 were passed in. i am using $in Comparison operator { "$expr": { "$not": { "$eq":{ "$and": [ { "PrName": { "$in": [ "pname" ] } }, { "AccountId": { "$in": [ "34562", "88765", "87654", "12345" ] } } ] } } } } 回答1: When you use $expr , the operator expressions syntax changes a little: { $in: [ <expression>, <array expression> ] } { "$expr": { "$not": { "$and": [ { "$in": [ "$PrName", [