aggregation-framework

Mongodb multi nested array search

半腔热情 提交于 2019-12-20 02:42:28
问题 My aim is to search records of data userid 1 Below is my data { "_id" : 2, "name" : "test", "data" :[{"_id" : "1","file" : "nic", "userid" : [1,2 ]}, {"_id" : "2","file" : "nic1","userid" : [1 ] }, {"_id" : 3,"file" : "nick2","userid" : [1,2 ]} ]}, { "_id" : 3, "name" : "test", "data" : [{"_id" : "1","file" : "nic","userid" : [1,2 ] }, {"_id" : "2","file" : "nic1", "userid" : [3,2 ] } ]} out put should be { "_id" : 2, "name" : "test", "data" :[{"_id" : "1","file" : "nic", "userid" : [1,2 ]},

Reverse array field in MongoDB

懵懂的女人 提交于 2019-12-20 01:09:21
问题 I have a collection with a location field that was entered in the wrong order: location: [38.7633698, -121.2697997] When I try to place a 2d index on the field using ... db.collection.ensureIndex({'location': '2d'}); ... I get the following error because the latitude and longitude are reversed. "err" : "location object expected, location array not in correct format", "code" : 13654 How can I reverse this array for each document in the mongo shell? 回答1: db.loc.find().forEach(function (doc) {

MongoDB query : $near with aggregation

最后都变了- 提交于 2019-12-19 17:35:10
问题 I have a collection that looks like that { "_class" : "User", "_id" : "id1", "places" : [ { "_id" : "1", "address" : "test1", "location" : { "latitude" : 1, "longitude" : 1 } }, { "_id" : "2", "address" : "test2", "location" : { "latitude" : 2, "longitude" : 2 } },... ] } I am trying to retrieve every place of a user (in a range of 2 km). This query doesn't work : db.users.ensureIndex({"places.location":"2d"}) db.users.aggregate([ {$match : { "_id" : "id1" } }, {$unwind : "$places"}, {

Zip arrays with MongoDB

Deadly 提交于 2019-12-19 09:33:50
问题 Is it possible to zip arrays within a mongo document? I mean the functional programming definition of zip, where corresponding items are paired into tuples. To be more precise, I would like start with a mongo document like this: { "A" : ["A1", "A2", "A3"], "B" : ["B1", "B2", "B3"], "C" : [100.0, 200.0, 300.0] } and end up with mongo documents like these: {"A":"A1","B":"B1","C":100.0}, {"A":"A2","B":"B2","C":200.0}, {"A":"A3","B":"B3","C":300.0}, Ideally this would use the aggregation

How to ORDER BY FIELD VALUE in MongoDB

三世轮回 提交于 2019-12-19 09:28:45
问题 In Mysql I often use the FIELD() function in the ORDER BY clause: ORDER BY FIElD(id, '1', '6', '3', ...); How does one get the same results in MongoDB? I tried the following: .find(...).sort({id: [1, 6, 3]}) This did not work 回答1: So for the record: Given the array [1,6,3] what you want in your query is this: db.collection.aggregate([ { "$project": { "weight": { "$cond": [ { "$eq": ["_id": 1] }, 3, { "$cond": [ { "$eq": ["_id": 6] }, 2, { "$cond": [ { "$eq": ["_id": 3] }, 1, 0 ]}, ]}, ]} }},

MongoDB Aggregation Query- Rename Fields Returned from Within Embedded Documents

喜欢而已 提交于 2019-12-19 09:13:26
问题 I'm currently using aggregate operator to return documents that has an array of embedded (sub) documents. I want to rename the field name for the array and also rename fields names in the embedded documents of the array. As an example, for projection I want to rename the array from "friends" to "buddies" and I also want to rename fields in the embedded document from "name" to "nickName". Can I do this within an aggregate operation and if so how? Here's an example of the source document: [ {

MongoDB Aggregation Query- Rename Fields Returned from Within Embedded Documents

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 09:13:23
问题 I'm currently using aggregate operator to return documents that has an array of embedded (sub) documents. I want to rename the field name for the array and also rename fields names in the embedded documents of the array. As an example, for projection I want to rename the array from "friends" to "buddies" and I also want to rename fields in the embedded document from "name" to "nickName". Can I do this within an aggregate operation and if so how? Here's an example of the source document: [ {

pymongo group by datetime

余生长醉 提交于 2019-12-19 08:53:34
问题 Im trying to search through a collection and group records by date field which is a datetime. I know pymongo converts those to the proper type on the background (ISODate or something like that). Question is, since datetime objects have date, time, timezone.. how can i tell the group operator to use only the date portion? Because otherwise i dont get the desired grouping since time is preventing the records with same day, month, year to be grouped together. db.test.aggregate([ {"$group": { "

Operator $arrayElemAt in aggregation with Mongo < 3.2

浪子不回头ぞ 提交于 2019-12-19 07:39:31
问题 Using the aggregation framework in Mongo, How can I achieve the same result in Mongo < 3.2 as in Mongo 3.2 with the operation $arrayElemAt ? Example in Mongo 3.2 Collection { "_id" : 1, "name" : "dave123", favorites: [ "chocolate", "cake", "butter", "apples" ] } Query db.users.aggregate([ { $project: { name: 1, first: { $arrayElemAt: [ "$favorites", 0 ] }, last: { $arrayElemAt: [ "$favorites", -1 ] } } } ]) It works fine But, I have been forced to used Mongo 3.0 so I can't use this operator,

Mongodb unwind nested documents

女生的网名这么多〃 提交于 2019-12-19 07:35:20
问题 I have a document in MongoDB and I'm trying to unwind it in PHP. I want to unwind a document that has a subdocument that contains yet anothersubdocument. I was able to do this succesfully if the document only contains strings and numbers, but if it contains another subdocument then I can't get it to work. I get this error: exception: $unwind: value at end of field path must be an array Can you not unwind a subdocument that contains another level of subdocument? If not, how would you go about