mongodb-query

Mongodb : array element projection with findOneAndUpdate doesn't work?

狂风中的少年 提交于 2019-12-18 04:25:13
问题 I am using Mongoose and I'm trying to update an array element and get it back updated. This is my document structure : { name:String, friends:[ { name:String, age:Number } ] } When I execute the following query I get all the friends in the result but I only want to get 25 year old friends back : theCollection.findOneAndUpdate( { name : 'cherif', 'friends.name':'kevin' }, { $set:{ 'friends.$.age':25 } }, { friends: { $elemMatch: { age : 25 } } }, function(err,result){ if (!err) { console.log

Difference between decorating a property in C# with BsonRepresentation(BsonType.ObjectId) vs BsonId vs ObjectId

风流意气都作罢 提交于 2019-12-18 04:08:20
问题 Am new to mongodb and am liking how easy it is not to worry about schema stuff, I have a question suppose you want an Id property in mongo and mongo uses ObjectId to denote property Id's, so far i see you can have or decorate an Id as follows, public ObjectId Id {get; set;} //or [BsonId] public string Id {get; set;} //or [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id {get; set;} Can anyone explain to me why most people choose the last type, and whats going on and how the

Use $gte and &lte mongo operator if date is in string format in mongodb

℡╲_俬逩灬. 提交于 2019-12-18 03:47:18
问题 I have dob field like {dob:"02-23-2000"} in database. Now I want to perform $gte and &lte on dob field that is in string format like below: db.panelists.count({"dob":{ '$gte': '08-02-1998', '$lte': '08-02-2003' }}); I am getting count value as zero always. Could anyone please help me out to resolve this query in same dob format. 回答1: You can try below aggregation in mongodb 3.6 You can use $dateFromString aggregation to convert your dob from string to date and then easily $count the documents

MongoDB Aggregation unwind more than one array

家住魔仙堡 提交于 2019-12-18 03:42:01
问题 This is what my documents look like { code: "A2df", clicks: 7, countries: [{"country":"IN", clicks:5},{"country":"US", clicks:2}], domains: [{"domain":"a.com", clicks:4},{"country":"b.com", clicks:3}] }, { code: "B3ws", clicks: 11, countries: [{"country":"IN", clicks:6},{"country":"ND", clicks:5}], domains: [{"domain":"a.com", clicks:7},{"country":"c.com", clicks:4}] }, { code: "A2df", clicks: 5, countries: [{"country":"IN", clicks:2},{"country":"ND", clicks:3}], domains: [{"domain":"a.com",

Finding Closest Points to a certain Point Given its Coordinates and Maximum Distance - Query Result Undefined using Mongoose with MEAN Stack

岁酱吖の 提交于 2019-12-18 03:17:08
问题 I've an issue that I wasn't able to solve in some days, even looking at related Stack Overflow Q/A. I'm developing an application reusing Scotch's Create a MEAN Stack Google Map App Tutorial by Ahmed Haque approach. I'm trying to implement an application that uses Google Maps API to draw Points , LineStrings and Polygons which coordinates are contained in GeoJson files that are stored in a MongoDB instance. I'm using Mongoose to build the Schema for my data and query my MongoDB database. I

Obtaining $group result with group count

喜你入骨 提交于 2019-12-17 23:33:48
问题 Assuming I have a collection called "posts" (in reality it is a more complex collection, posts is too simple) with the following structure: > db.posts.find() { "_id" : ObjectId("50ad8d451d41c8fc58000003"), "title" : "Lorem ipsum", "author" : "John Doe", "content" : "This is the content", "tags" : [ "SOME", "RANDOM", "TAGS" ] } I expect this collection to span hundreds of thousands, perhaps millions, that I need to query for posts by tags and group the results by tag and display the results

Mongoose sort the aggregated result

…衆ロ難τιáo~ 提交于 2019-12-17 22:59:22
问题 I'm having a lot of difficulty in solving this mongodb (mongoose) problem. There is this schema 'Recommend' (username, roomId, ll and date) and its collection contains recommendation of user. I need to get a list of most recommended rooms (by roomId). Below is the schema and my tried solution with mongoose query. var recommendSchema = mongoose.Schema({ username: String, roomId: String, ll: { type: { type: String }, coordinates: [ ] }, date: Date }) recommendSchema.index({ ll: '2dsphere' });

MongoDB elemmatch multiple elements in array

邮差的信 提交于 2019-12-17 22:52:16
问题 I have an mongodb document like { "_id" : ObjectId("54e66b2da7b5f3a92e09dc6c"), "SomeMetric" : [ { //some object } { //some object } ], "FilterMetric" : [ { "min" : "0.00", "max" : "16.83", "avg" : "0.00", "class" : "s1" }, { "min" : "0.00", "max" : "16.83", "avg" : "0.00", "class" : "s2" }, { "min" : "0.00", "max" : "16.83", "avg" : "0.00", "class" : "s1" }, { "min" : "0.00", "max" : "16.83", "avg" : "0.00", "class" : "s2" } ] } Typically it contains many nested arrays like this. I want to

How to Group mongodb - mapReduce output?

邮差的信 提交于 2019-12-17 21:06:23
问题 i have a query regarding the mapReduce framework in mongodb, so i have a result of key value pair from mapReduce function , now i want to run the query on this output of mapReduce. So i am using mapReduce to find out the stats of user like this db.order.mapReduce(function() { emit (this.customer,{count:1,orderDate:this.orderDate.interval_start}) }, function(key,values){ var sum =0 ; var lastOrderDate; values.forEach(function(value) { if(value['orderDate']){ lastOrderDate=value['orderDate']; }

mongoDB upsert on array

喜夏-厌秋 提交于 2019-12-17 20:52:37
问题 my db model looks like this "clientId":"123456" "devices" : [{ "deviceId" : "123", "deviceType" : "ios", "notification" : true, } { "deviceId" : "321", "deviceType" : "android", "notification" : true, } ] The c# model I pass to my DB method has ClientId,DeviceId,DeviceType,notification. Note that it doesn't have an array of devices the behaviour I'm trying to achieve (working) If ClientId is not yet in the database, create a new record in db with the passed value,(devices array will have one