mongodb-query

MongoDB: $lookup returns empty array [duplicate]

拜拜、爱过 提交于 2019-12-17 20:26:06
问题 This question already has answers here : Mongoose always returning an empty array NodeJS (1 answer) Querying after populate in Mongoose (6 answers) Closed last year . I have two models, user and schedule, and i want to combine these two using $lookup and mongoose. User (model) name: { type: String, required: true }, firstName: { String }, lastName: { String }, storeKey: { type: String, required: true }, avatar: String, birthday: String, phone: { type: String }, doc: String, email: { type:

MongoDB Aggregate Sum Each Key on a Subdocument

本秂侑毒 提交于 2019-12-17 20:24:48
问题 I have multiple documents with this schema, each document is per product per day: { _id:{}, app_id:'DHJFK67JDSJjdasj909', date:'2014-08-07', event_count:32423, event_count_per_type: { 0:322, 10:4234, 20:653, 30:7562 } } I would like to get the sum of each event_type for a particular date_range. This is the output I am looking for where each event type has been summed across all the documents. The keys for event_count_per_type can be anything, so I need something that can loop through each of

MongoDB count distinct value?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 20:12:03
问题 Here below show my code. I have to calculate the how many times distinct value repeated. Here i have store distinct value in "results".I used collection.count() to calculate but it's not work. please any one tell me where i have to mistake. Thank you very much . var DistinctIntoSingleDB = function(Collection,arr,opt,distVal,callback){ Collection.find({}).distinct(distVal, function(err, results) { if(!err && results){ console.log("Distinct Row Length :", results.length); var a,arr1 = []; for

Map an 'array of objects' to a simple array of key values

你说的曾经没有我的故事 提交于 2019-12-17 19:49:24
问题 I am new to the mongoDB aggregation pipeline and have a really basic question but could not find the answer anywhere. I would like to simply convert the following block: "exclude" : [ { "name" : "Accenture" }, { "name" : "Aon Consulting" } ] to: "exclude" : [ "Accenture", "Aon Consulting" ] using the aggregation pipeline but I cannot seem to find how to do it even after going through the documentation on https://docs.mongodb.com/manual/reference/operator/aggregation/. Thanks for your help.

Compare two date fields in MongoDB

心已入冬 提交于 2019-12-17 19:38:13
问题 in my collection each document has 2 dates, modified and sync. I would like to find those which modified > sync, or sync does not exist. I tried {'modified': { $gt : 'sync' }} but it's not showing what I expected. Any ideas? Thanks 回答1: You can not compare a field with the value of another field with the normal query matching. However, you can do this with the aggregation framework: db.so.aggregate( [ { $match: …your normal other query… }, { $match: { $eq: [ '$modified', '$sync' ] } } ] ); I

MongoDB and C# Find()

99封情书 提交于 2019-12-17 19:23:16
问题 I have the below code and I am new to mongodb, I need help in finding an specific element in the collection. using MongoDB.Bson; using MongoDB.Driver; namespace mongo_console { public class User { public ObjectId Id { get; set; } public string name { get; set; } public string pwd { get; set; } } class Program { static void Main(string[] args) { MongoClient client = new MongoClient(); MongoServer server = client.GetServer(); MongoDatabase db = server.GetDatabase("Users"); MongoCollection<User>

Querying a MongoDB based on Mongo ID in a node.js app

依然范特西╮ 提交于 2019-12-17 19:14:16
问题 I'm using a node.js and mongodb, and I'm trying to query the database based on the mongo generated ID using the following: collection.findOne( {_id:doc._id} , function(err, item) {}); I am 100% certain that my doc._id is an exact match to the doc _id that I am looking for in the collection, and yet I get a null response from the db query. I have tried this using other keys in the document and it returns the document just fine. It's only when I try to use the mongo ID. 回答1: The MongoDb is an

Mongodb inserting doc without _id field

让人想犯罪 __ 提交于 2019-12-17 18:42:19
问题 I am newbie to mongodb. I need to insert a doc without the _id field generating automatically. I need to set the field Tenant_id as unique or need to change the "_id" field to Tenant_id. how to do it? something like this Tenant {Tenant_id: 123, Tenant_info: ...} 回答1: By default, all regular collections automatically insert an _id field if it is absent. However, this behavior can be changed when you create the collection, by setting explicitely the autoIndexId parameter to false. > db

MongoDB Projection of Nested Arrays

亡梦爱人 提交于 2019-12-17 18:26:03
问题 I've got a collection "accounts" which contains documents similar to this structure: { "email" : "john.doe@acme.com", "groups" : [ { "name" : "group1", "contacts" : [ { "localId" : "c1", "address" : "some address 1" }, { "localId" : "c2", "address" : "some address 2" }, { "localId" : "c3", "address" : "some address 3" } ] }, { "name" : "group2", "contacts" : [ { "localId" : "c1", "address" : "some address 1" }, { "localId" : "c3", "address" : "some address 3" } ] } ] } Via q = { "email" :

How to calculate the running total using aggregate

跟風遠走 提交于 2019-12-17 18:25:02
问题 I'm developing a simple financial app for keeping track of incomes and outcomes. For the sake of simplicity, let's suppose these are some of my documents: { "_id" : ObjectId("54adc0659413535e02fba115"), "description" : "test1", "amount" : 100, "dateEntry" : ISODate("2015-01-07T23:00:00Z") } { "_id" : ObjectId("54adc21a0d150c760270f99c"), "description" : "test2", "amount" : 50, "dateEntry" : ISODate("2015-01-06T23:00:00Z") } { "_id" : ObjectId("54b05da766341e4802b785c0"), "description" :