mongodb-query

In Mongo, how do I only display documents with the highest value for a key that they share?

会有一股神秘感。 提交于 2019-12-08 08:30:23
问题 Say I have the following four documents in a collection called "Store": { item: 'chair', modelNum: 1154, votes: 75 } { item: 'chair', modelNum: 1152, votes: 16 } { item: 'table', modelNum: 1017, votes: 24 } { item: 'table', modelNum: 1097, votes: 52 } I would like to find only the documents with the highest number of votes for each item type. The result of this simple example would return modelNum: 1154 and modelNum: 1097. Showing me the most popular model of chair and table, based on the

Grouping documents in pairs using mongo aggregation

只愿长相守 提交于 2019-12-08 07:26:58
问题 I have a collection of items, [ a, b, c, d ] And I want to group them in pairs such as, [ [ a, b ], [ b, c ], [ c, d ] ] This will be used in calculating the differences between each item in the original collection, but that part is solved using several techniques such as the one in this question. I know that this is possible with map reduce, but I want to know if it's possible with aggregation. Edit: Here's an example, The collection of items; each item is an actual document. [ { val: 1 }, {

Insert Into Array of object MongoDB

a 夏天 提交于 2019-12-08 07:14:55
问题 I have { "_id" : ObjectId("5a25af80d2c4dd065991dccc"), "username" : "abc@gmail.com", "password" : "$2a$10$ptFyLKtyyrL5gMYdXS6wV.HLUA4Py5iudMJDldf5qsHFS4.9TPCyy", "role" : "Admin", "__v" : 0, "list" : [ { "name" : "We", "arr" : [ "5a26d554677475818a795f75", "5a395bb0976d5304c07f7dd4" ] }, { "name" : "sandeep", "arr" : [ "5a26d554677475818a795f75" ] } ] } I want to add an element inside list.arr where name = 'we' and add only if the element does not exist how do i perform this query. 回答1: if i

Querying array of objects in MongoDB using only find method

╄→尐↘猪︶ㄣ 提交于 2019-12-08 07:10:36
问题 Look the following documents in a contacts collection on MongoDB 3.4: { "_id" : ObjectId("58f045526320ef24fc61fdb2"), "name" : "John Doe", "tags" : [ { "name": "tagA", "created_at": ISODate("2017-01-27T10:30:00Z") }, { "name": "tagB", "created_at": ISODate("2017-01-28T13:30:00Z") } ], }, { "_id" : ObjectId("58f045526320ef24fc61fdb3"), "name" : "Johnny Doe", "tags" : [ { "name": "tagA", "created_at": ISODate("2016-12-21T19:30:00Z") }, { "name": "tagC", "created_at": ISODate("2017-01-28T13:30

How to retrieve all matching elements present inside array in Mongo DB?

痞子三分冷 提交于 2019-12-08 06:39:59
问题 I have document shown below: { name: "testing", place:"London", documents: [ { x:1, y:2, }, { x:1, y:3, }, { x:4, y:3, } ] } I want to retrieve all matching documents i.e. I want o/p in below format: { name: "testing", place:"London", documents: [ { x:1, y:2, }, { x:1, y:3, } ] } What I have tried is : db.test.find({"documents.x": 1},{_id: 0, documents: {$elemMatch: {x: 1}}}); But, it gives first entry only. 回答1: As JohnnyHK said, the answer in MongoDB: select matched elements of

Limit aggregation in grouped aggregation

和自甴很熟 提交于 2019-12-08 06:26:08
问题 I had a collection like this, but with much more data. { _id: ObjectId("db759d014f70743495ef1000"), tracked_item_origin: "winword", tracked_item_type: "Software", machine_user: "mmm.mmm", organization_id: ObjectId("a91864df4f7074b33b020000"), group_id: ObjectId("20ea74df4f7074b33b520000"), tracked_item_id: ObjectId("1a050df94f70748419140000"), tracked_item_name: "Word", duration: 9540, } { _id: ObjectId("2b769d014f70743495fa1000"), tracked_item_origin: "http://www.facebook.com", tracked_item

How to get this result with aggregate in mongoDB

孤人 提交于 2019-12-08 06:15:36
问题 I have an invoice collection... { "_id" : 1, "items" : [ { "_id" : 1, "value" : 100, "price" : 9500 }, { "_id" : 2, "value" : 200, "price" : 9500 } ], "costs" : [ { "_id" : 1, "price" : 100 }, { "_id" : 2, "price" : 150 }, { "_id" : 3, "price" : 250 } ]} I get Invoice amount with aggregate... In other words => {$sum : {$multiply :{[$items.value, $items.price]} }} - {$sum : "$costs.price"}; { "_id" : 1, "amount" : 2849500 } very thinks... ;-) 回答1: Mongo 3.4 Version $map to multiply the item's

How to Get an Aggregate from a MongoDB Collection

我怕爱的太早我们不能终老 提交于 2019-12-08 05:28:29
The following collection contains the ticks for the diverse exchange symbols (in this case BTCUSD and BTCEUR ): { "_id" : ObjectId("5a08d2b956df9b2302759d1a"), "symbol" : "BTCUSD", "time" : ISODate("2013-04-13T00:00:00Z"), "open" : 112, "close" : 91.1, "high" : 130, "low" : 81.12, "volume" : 23866.6770456 } { "_id" : ObjectId("5a08d2b956df9b2302759d1c"), "symbol" : "BTCUSD", "time" : ISODate("2013-04-14T00:00:00Z"), "open" : 91.1, "close" : 90.171, "high" : 109, "low" : 20, "volume" : 16437.2196645 } { "_id" : ObjectId("5a08d2b956df9b2302759d1e"), "symbol" : "BTCEUR", "time" : ISODate("2013-04

Geospatial $near within current document field value

天大地大妈咪最大 提交于 2019-12-08 05:11:41
问题 Take this query: { 'location' : { '$near' : [x,y], '$maxDistance' : this.field } } I want to assign $maxDistance the value of the specified field from the current evaluated document. Is that possible? 回答1: Yes it's possible. You just use $geoNear instead. Beware the catches and read carefully. Presuming that your intent to is to store a field such as "travelDistance" to indicate on the document that any such searches must be "within" that supplied distance from the queried point to be valid.

Group Distinct Values and Counts for Each Property in One Query

好久不见. 提交于 2019-12-08 04:50:32
问题 I have a data in profile collection [ { name: "Harish", gender: "Male", caste: "Vokkaliga", education: "B.E" }, { name: "Reshma", gender: "Female", caste: "Vokkaliga", education: "B.E" }, { name: "Rangnath", gender: "Male", caste: "Lingayath", education: "M.C.A" }, { name: "Lakshman", gender: "Male", caste: "Lingayath", education: "B.Com" }, { name: "Reshma", gender: "Female", caste: "Lingayath", education: "B.E" } ] here I need to calculate total Number of different gender, total number of