mongodb-query

MongoDB: Slow query, even with index

天大地大妈咪最大 提交于 2019-12-05 00:30:33
问题 I have a webpage, which uses MongoDB for storing and retrieving various measurements. Suddenly, in some point, my webpage became so sluggish it became unusable. It turns out, my database is the culprit. I searched for and have not found any solution for my problem, and I apologize, as I am pretty new to MongoDB and pulling my hair out at the moment. Version of MongoDB I am using is 2.4.6, on VM Machine with 20GB RAM, which runs Ubuntu server 12.04. There is no replica or sharding set up.

Mongo db aggregation multiple conditions

点点圈 提交于 2019-12-05 00:23:48
I want to project a collection applying exporting a value only if a field is inside a range. Sort of: db.workouts.aggregate({ $match: { user_id: ObjectId(".....") } }, { $project: { '20': { $cond: [ {$gt: [ "$avg_intensity", 20]} , '$total_volume', 0] } } }) I need to get the value only if the avg_intensity is inside a certain range. I will then group and sum on the projection result. What I am trying to do is applying a $gt and $lt filter but with no much success. db.workouts.aggregate( { $match: { user_id: ObjectId("....") } }, { $project: { '20': { $cond: [ [{$gt: [ "$avg_intensity", 20]},

Mongo aggregation cursor & counting

喜夏-厌秋 提交于 2019-12-05 00:02:08
According to the mongodb node driver docs the aggregate function now returns a cursor (from 2.6). I hoped that I could use this to get a count of items pre limit & skipping but there doesn't seem to be any count function on the created cursor. If I run the same queries in the mongo shell the cursor has an itcount function that I can call to get what I want. I saw that the created cursor has an on data event (does that mean it's a CursorStream ?) which seemed to get triggered the expected number of times, but if I use it in combination with cursor.get no results get passed into the callback

Check If field exists in an sub-document of an Array

痞子三分冷 提交于 2019-12-04 23:07:28
I have a schema that is similar to this. {id: Number, line_items: [{ id: String, quantity: Number, review_request_sent :Boolean }], total_price: String, name: String, order_number: Number } What I want to find is all documents that don't have the field review_request_sent set for all items in the array. Example { id: 1, line_items: [ { id: 43, review_request_sent: true } ] }, { id: 2, line_items: [ { id: 1, review_request_sent: false }, { id: 39 }, ] }, { id: 3, line_items: [ { id: 23, review_request_sent: true }, { id: 85, review_request_sent: true }, { id: 12, review_request_sent: false } ]

Create a conditional TTL in mongo

故事扮演 提交于 2019-12-04 22:31:17
问题 There is a particular task I want to accomplish , but I am not finding any particular way to do that. Lets say I have an app that is used to send mails. I keep record of this mails in a collection in mongo. And using this app I can send mail right now or can schedule mails for future. The structure of documents in collection is like : { '_id' : 123456789, 'to_email' : 'xyz@gmail.com' 'from_email' : 'abc@gmail.com' 'subject': 'some subject' 'type' : '<1 if normal and 2 if scheduled>',

mongodb query by sub-field

可紊 提交于 2019-12-04 22:27:29
How to query all {"module" : "B"} ? The following query doesn't work: db.XXX.find({ "_id" : { "module" : "B" } }); Thanks a ton! There data looks like: { "_id" : {"module" : "A","date" : ISODate("2013-03-18T07:00:00Z")}, "value" : {"count" : 1.0} } { "_id" : {"module" : "B","date" : ISODate("2013-03-18T08:00:00Z")}, "value" : {"count" : 2.0} } Try: db.XXX.find({ "_id.module" : "B" }); The difference is your original query would be trying to match on that entire subdocument (i.e. where _id is a subdocument containing a "module" field with value "B" and nothing else) Reference: MongoDB Dot

Understanding mongo db explain

。_饼干妹妹 提交于 2019-12-04 22:18:43
I fired a query and tried to explain it on mongo console and got "isMultiKey" : true, "n" : 8, "nscannedObjects" : 17272, "nscanned" : 17272, "nscannedObjectsAllPlans" : 21836, "nscannedAllPlans" : 21836, "scanAndOrder" : true, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 184, Most of the things are explained in http://www.mongodb.org/display/DOCS/Explain , but I cannot understand what does nscannedObjectsAllPlans, nscannedAllPlans means. Can anyone help? Thanks nscanned and nscannedObjects report results for the winning plan. nscannedAllPlans and nscannedObjectsAllPlans

How to convert below MongoDB query into PHP?

我与影子孤独终老i 提交于 2019-12-04 21:54:20
Below query worked fine in Studio 3T and Robomongo, But I want to convert it into PHP format, Following is my query, db.news.aggregate([ { $match: { "_id" : "1" } }, { "$project": { "comments": { "$filter": { "input": "$comments", "as": "comment", "cond": { "$eq": [ "$$comment.status", "active" ] } } } } }, { "$project": { "comments": { "$slice": [ { "$slice": [ "$comments", { "$subtract": [ { "$size": [ "$comments" ] }, 1 ] } ] }, -1 ] } } }]) I have tried below, But it giving error " Error: localhost:27017: FieldPath field names may not be empty strings. " PHP converted sample: <?php

Array of ObjectIds in Mongoose 3.8

北战南征 提交于 2019-12-04 21:48:51
I have a schema called Message and it has a property replies which are also Message objects. I am trying to define it in mongoose, but replies keeps returning undefined. var MessageSchema = new Schema({ sender: { type: Schema.ObjectId, ref: 'User' }, replies: [{type:Schema.ObjectId, ref:'Message'}], roomId: String, sendTime: Date, content: String, parentId: Schema.ObjectId }); I've also tried replies: [MessageSchema] and replies:[Schema.ObjectId] but all of them keep returning undefined. Nothing wrong with the code you are listing so it's what you are not showing us that causes the problems.

matching fields internally in mongodb

旧时模样 提交于 2019-12-04 21:02:14
问题 I am having following document in mongodb { "_id" : ObjectId("517b88decd483543a8bdd95b"), "studentId" : 23, "students" : [ { "id" : 23, "class" : "a" }, { "id" : 55, "class" : "b" } ] } { "_id" : ObjectId("517b9d05254e385a07fc4e71"), "studentId" : 55, "students" : [ { "id" : 33, "class" : "c" } ] } Note: Not an actual data but schema is exactly same. Requirement: Finding the document which matches the studentId and students.id (id inside the students array using single query . I have tried