mongodb-query

How do I return group results by multiple fields in MongoDB?

早过忘川 提交于 2019-12-11 15:53:41
问题 In MongoDB, the aggregate query below gives the average price per product-category, where business-name is "Foobar Inc" . var match = { $match: {"businessName": "FooBar Inc"}, ; var group = { $group: { _id: "$productCategory", total: { $avg: "$price" }}} var results = await db.aggregate("productsCollection", limit, match, group); Example object from productCollection : {"businessName": "FooBar Inc.", "productCategory": "toys", "price":88} Output: [{"_id": "shoes", "total":97}, {"_id": "toys",

MongoDB: How to index unknown fields

放肆的年华 提交于 2019-12-11 15:49:16
问题 We're storing documents with a undefined structure. I mean, it has a basic structure ( id , user and creationTimestamp ), but is also there a Map<String, Object> values fields, where we are able to store whichever structure: public class Metadata { private String id; private String user; private Date creationTimestamp; private Map<String, Object> values; } Example: > db.metadata.find(); { "_id" : "Doc2Ref2Mdt1", "user" : "user1", "creationTimestamp" : ISODate("2018-09-24T12:20:56.958Z"),

MongoDB: what is the difference between $elemMatch and $and to find objects inside array?

佐手、 提交于 2019-12-11 15:41:17
问题 Is there any logical difference between the usage of the query operator $and db.collection.find({$and: [{"array.field1": "someValue"}, {"array.field2": 3}]}) and the usage of the projection operator $elemMatch db.collection.find({array: {$elemMatch: {field1: "someValue", field2: 3}}}) to find documents which contain the object fields inside an array? 回答1: Your first query will find documents, where array have at least one element with field1= somevalue and at least one element with field2=3.

how to use $regex search in referenced field in mongodb

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 15:34:49
问题 i am struggling with a task, which it is I used two schemas User Schema { "first_name": String, "last_name":String, "address": String } Employee schema { user:{ type: ObjectId, ref: 'User' }, gross_pay: String, net_pay: String tax: String, } Well, how can I search first_name using $regex in Employee Schema in this user referenced field? I tried in so many ways, can't get it. how can i resolve it? Thanks in advance 回答1: First Approach: Using $lookup aggregation Employee.aggregate([ { "$lookup"

How to join the two table, it is in two different DB in mongodb

馋奶兔 提交于 2019-12-11 15:05:17
问题 I have the documents in following structure saved in my mongodb. DB : DB1, Collection: Collection1 { "_id":"111", "studentID" : "1" } DB : DB2, Collection: Collection2 { "_id":"111", "studentID" : "1", "login" : true }, { "_id":"333", "studentID" : "2", "login" : false } my question is i want join both table, then DB1-> Collection1 i have one document studentID is 1, but DB2-> Collection2 have 2 docs but i have to return one document which equal to DB1-> Collection1 mongodb version = 3.4 My

How to combine $elemMatch with $nin or $not in a MongoDB query?

丶灬走出姿态 提交于 2019-12-11 15:02:22
问题 I want to find all results where the array fieldB does not have values (val1 and val2) in fieldC objects. I have tried: db.SalesToImport .find( { fieldA: {$exists:true}, ,fieldB: {$not: {$elemMatch: {fieldC: 'val1'}}} ,fieldB: {$not: {$elemMatch: {fieldC: 'val2'}}} } ); And I have tried: db.SalesToImport .find( { fieldA: {$exists:true}, ,fieldB: {$not: {$elemMatch: {fieldC: 'val1', fieldC: 'val2'}}} } ); They both yield the same results and while they remove some documents, there are still

Group by day with Multiple Date Fields

一世执手 提交于 2019-12-11 14:32:17
问题 I have documents stored into MongoDB like this : { "_id" : "XBpNKbdGSgGfnC2MJ", "po" : 72134185, "machine" : 40940, "location" : "02A01", "inDate" : ISODate("2017-07-19T06:10:13.059Z"), "requestDate" : ISODate("2017-07-19T06:17:04.901Z"), "outDate" : ISODate("2017-07-19T06:30:34Z") } And I want give the sum, by day, of inDate and outDate. I can retrieve of both side the sum of documents by inDate day and, on other side, the sum of documents by outDate , but I would like the sum of each.

Does a MongoDB cursor “auto-grow” when I add documents

独自空忆成欢 提交于 2019-12-11 14:22:46
问题 I am using a MongoDB cursor to find a large number of documents, which takes quite some time. What happens if during this time, there are documents added to the database that match the search criteria of the cursor. Will the cursor return the documents? Or does the cursor take some kind of snapshot if it begins, and thus omits the later added results? 回答1: Will the cursor return the documents? Yes. This also happens when you update some documents which you received from the cursor, causing

MongoDB aggregate count of items in array across different documents?

北慕城南 提交于 2019-12-11 13:57:25
问题 Here is my MongoDB collection schema: company: String model: String tags: [String] I need to aggregate it so I get the following output: [{ "_id": { "company": "Lenovo", "model": "T400" }, "tags": { tag: "SomeTag" count: 124 // number of times, this tag was found in `Lenovo T400` } }...] I tried to do the following: var aggParams = {}; aggParams.push({ $unwind: '$tags' }); aggParams.push({ $group: { _id: { company: '$company', model: '$model' }, tags: { $push: { tag: '$tags', count: { $sum: 1

Return Collection names based on column value in Mongo

邮差的信 提交于 2019-12-11 13:15:25
问题 I have 5 collections C1, C2, C3, C4 and N5 each having a last_updated value. db.getCollectionNames().filter(function(c){ return c.indexOf('C')==0}) will return me all the collections that start with "C". Is it possible to go one step ahead and do a filter based on "last_updated" column so that i will get all "C" collections that are updated yesterday. something like this db.getCollectionNames().filter(function(c){ return (db.c.find({update_on: "01-Dec-2014"}).count() > 1)}); Eventhough the