mongodb-query

MongoDB aggregate result with two different keys

元气小坏坏 提交于 2019-12-11 10:14:58
问题 I have recently put this question that helped me to obtain an array for obtaining a result in an aggregation mongoDB query ( MongoDB Aggregate Array with Two Fields ) My problem right now is to obtain combine two different keys. I have a products collection with general products on it: { "_id" : ObjectId("554b9f223d77c810e8915539"), "brand" : "Airtex", "product" : "E7113M", "type" : "Fuel Pump", "warehouse_sku" : [ "1_5551536f3d77c870fc388a04", "2_55515e163d77c870fc38b00a" ] } And I have the

group by date in mogodb query without considering time

十年热恋 提交于 2019-12-11 10:06:07
问题 I have the following code: DBObject groupFields = new BasicDBObject("_id", "$Date"); groupFields.put("count", new BasicDBObject("$sum", 1)); DBObject groupBy = new BasicDBObject("$group", groupFields); stages.add(groupBy); DBObject project = new BasicDBObject("_id", 0); project.put("count", 1); project.put("Date", "$_id"); stages.add(new BasicDBObject("$project", project)); DBObject sort = new BasicDBObject("$sort", new BasicDBObject("Date", 1)); stages.add(sort); to group by Date and show

Update all sub-documents inside document mongodb

亡梦爱人 提交于 2019-12-11 10:03:24
问题 I want something like this db.students.update( { "_id": 4, "grades.grade": 85 }, { "$set": { "grades.$.std" : 6 } } ) But I want to update all "grades" not just the first one, like the $ explain when I tried it just update the first one. any ideas about to update the entire list? 回答1: You can't update all the elements of an array in a document using a single mongodb query, you can use following code for your purpose db.students.find({"grades.grade": 85 }).forEach(function(item){ //iterate

Use $stdDevSamp or $stdDevPop with Spring Mongo

对着背影说爱祢 提交于 2019-12-11 09:56:14
问题 I'd like to know how to implement a Standard Deviation aggregation Function to use in Spring Mongo Data. I Know Mongo DB 3.2 has a Standard Deviation aggregation function, but It isn't available in Spring Data. Could I use the Mongo's aggregation function? Thanks. 回答1: There is a distinct difference between "not available" and "no implemented helper method" , and that is the real case here. Just because there is no "helper" for implementing the $stdDevSamp or $stdDevPop operators, does not

findAndModify Error in mongodb - nodejs - error code 17287

纵饮孤独 提交于 2019-12-11 09:48:57
问题 I am getting following error : MongoError: exception: nextSafe(): { $err: "Can't canonicalize query: B adValue bad sort specification", code: 17287 } functions.getNextIndex = function(callback){ db.collection('counters').findAndModify( {_id:'productId'}, {$inc: {sequence_value:1}}, function(err,data){ if(!err) callback(data); else callback(err); }); } 回答1: It seems you are missing the "sort" argument in your query. Try something like: db.collection('counters').findAndModify( {_id:'productId'}

Sum array elements in Mongodb aggregation

你。 提交于 2019-12-11 09:45:40
问题 I have the following collection with c array inside each document { { id: 1, k: 2.2, type: "dog", c: [ {parentId:1, p:2.2}, {parentId:1, p:1.4} ] }, { id: 2, k: 4.3, type:"cat", c: [ {parentId:2, p:5.2}, {parentId:2, p:4.5} ] } } parentId inside each subdocument in c is id of containing document. I want to group all docs by type and in each group know sum of k and sum of all p in all arrays of the group. Currently I do summation of k in the group stage but summation of p in the result array

MongoDB: How to rename a field using regex

霸气de小男生 提交于 2019-12-11 09:29:40
问题 I have a field in my documents, that is named after its timestamp, like so: { _id: ObjectId("53f2b954b55e91756c81d3a5"), domain: "example.com", "2014-08-07 01:25:08": { A: [ "123.123.123.123" ], NS: [ "ns1.example.com.", "ns2.example.com." ] } } This is very impractical for queries, since every document has a different timestamp. Therefore, I want to rename this field, for all documents, to a fixed name. However, I need to be able to match the field names using regex, because they are all

How can I get max value in nested documents?

允我心安 提交于 2019-12-11 09:20:02
问题 I have a collection(named menucategories ) in MongoDB 3.2.11: { "_id" : ... "menus" : [ { "code":0 }, { "code":1 }, { "code":2 }, { "code":3 } ] }, { "_id" : ... "menus" : [ { "code":4 }, { "code":5 }, { "code":6 }, { "code":7 } ] }, { "_id" : ... "menus" : [ { "code":8 }, { "code":9 }, { "code":10 }, { "code":11 } ] } Every menucategory has array named menus . And every menu(element of the array) has code . The ' code ' of menus is unique in every menu . I wanna get the maximum value of menu

Mongodb $lookup with nested document

六月ゝ 毕业季﹏ 提交于 2019-12-11 09:07:20
问题 I am trying to create a join using mongo's lookup. I have these three collections. orderTracking { _id: ObejctId("59fb7815b3b8429f4750b0df"), itemName : "Hamam Soap", TrackLocation: [{locationId: 1, at:"2017-10-11"}, {locationId: 2,at:"2017-10-13"}], userId : 12, price: 20 } locationType { _id: ObejctId("59b2111345cb72345a35fefd"), locationId : 1 productTypeName: "Warehouse" },{ _id: ObejctId("59af8ce445cb72345a35feea"), locationId : 2 productTypeName: "On Transit" } User { _id: ObejctId(

How to perform type casting in MongoDb aggregation query in version 3.6

陌路散爱 提交于 2019-12-11 08:57:26
问题 I have a field like this MyField : { "100": "1", "50" : "5" } And have another field which is int type. Call it IntField. I want to get the value of MyField according to the value of IntField, practically what I want is MyField[IntField]. I tried to do this like this way : $project: { Value : {$arrayElemAt: [{$filter: input:{$objectToArray:"$MyField"}, as : "m", cond:{$eq:["$$m.k","$IntField"]}} // k means key when i use objectToArray }, 0]}, } I expected output as 5 if IntField is 50 and 1