mongodb-query

Subtract $sum from Sub-document

别说谁变了你拦得住时间么 提交于 2019-12-20 06:27:26
问题 Given the following record in my MongoDB table: { "_id" : ObjectId("5a00c1c71680084c55811ae2"), "name" : "test", "tenantId" : "paul", "price" : 300, "deposits" : [ { "amount" : 100, "date" : ISODate("2017-11-07T14:08:19.324Z"), "_id" : ObjectId("5a01be55424b0f8922a5b472") }, { "amount" : 50, "date" : ISODate("2017-11-87T14:08:19.324Z"), "_id" : ObjectId("5a01be55424b0f8922a5b473") } ], "attention" : "", "due" : ISODate("2017-10-26T22:00:00.000Z") } I would like to filter all the records with

MongoDB : How to insert an embedded document in an array if a certain field is unique else update it

落爺英雄遲暮 提交于 2019-12-20 06:25:29
问题 I am trying to insert an embedded document if a certain field is unique else update it in mongoDB. Here is what I have so far: User.findOneAndUpdate( { _id : <idOfTheUserSavingTheContact>, //'arrayOfContacts.email' : <email of the contact> }, { $addToset: { arrayOfEmailsOfUnregisteredUsersWatchedByThisUser: { firstName : req.body.firstName, lastName : req.body.lastName, email : req.body.email, } } }, { new: true, upsert:true, runValidators: true}, function (err, updatedUser) { if(err){ return

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

家住魔仙堡 提交于 2019-12-20 05:56:58
问题 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

How to search sub arrays in MongoDB

♀尐吖头ヾ 提交于 2019-12-20 05:41:23
问题 I have this MongoDB collection: { "_id" : ObjectId("123"), "from_name" : "name", "from_email" : "email@mxxxx.com", "to" : [ { "name" : "domains", "email" : "domains@xxx.com" } ], "cc" : [ ], "subject" : "mysubject" } My goal is to search in this collection by the "to" with some email. 回答1: If you only want one field then MongoDB has "dot notation" for accessing nested elements: db.collection.find({ "to.email": "domains@example.com" }) And this will return documents that match: For more that

Find Count of all Overlapping Intervals

坚强是说给别人听的谎言 提交于 2019-12-20 05:39:09
问题 I have startTime and endTime for all records like this: { startTime : 21345678 endTime : 31345678 } I am trying to find number of all the conflicts. For example if there are two records and they overlap the number of conflict is 1. If there are three records and two of them overlap the conflict is 1. If there are three records and all three overlap the conflicts is 3 i.e [(X1, X2), (X1, X3), (X2, X3)] As an algorithm I am thinking of sorting the data by start time and for each sorted record

Remove Duplicates on mongodb

流过昼夜 提交于 2019-12-20 05:29:11
问题 I would like to remove duplicates on robomongo, my version 3.0.12 so I cant use DropDups, { "_id" : ObjectId("id"), "Name" : "No One", "SituationDate" : "18-03-2017", "Situation" : "ACTIVE", "Region" : "13 REGION", "RegisterNumber" : "7649", "Version" : "20170517" } The RegisterNumber should be unique so I would like to remove as duplicates by the RegisterNumber. EDIT :I just discovered that people from different regions can have the same registerNumber... How can I remove only those who have

Does Moongoose 3.8.8 support $position operator?

南楼画角 提交于 2019-12-20 04:55:44
问题 Does Moongoose 3.8.8 (the lastest version) support $position (http://docs.mongodb.org/manual/reference/operator/update/position/) operator from MongoDB 2.6.0? In the following code example the new elements is inserted in the end of the array userActivity.activities: model: var userActivity = new schema({ userId: {type:String, required:true, unique:true}, activities: [activity] }); var activity = new schema({ act: {type: Number, required:true}, }); query: var activity = { act: 1 }; model

Query by array type - MongoDB

不羁岁月 提交于 2019-12-20 04:40:23
问题 I have to query my mongoDB collection by type. Suppose I have these two documents of hello collection: { "_id" : ObjectId("56684ee0f597654b99d0d636"), "name" : "Scrooge", "surname" : "McDuck", "address" : { "road" : "Money Bin", "number" : 19 }, "hobbies" : [ "money", "food", "cooking" ] } { "_id" : ObjectId("66684ee0f597654b99d0d636"), "name" : "Mickey", "surname" : "Mouse", "address" : { "road" : "Topolinia", "number" : 34 }, "hobbies" : [ "minnie", "cheese" ] } Now, if I query by array

Aggregate documents where objects in array matches multiple conditions

﹥>﹥吖頭↗ 提交于 2019-12-20 04:19:25
问题 I have a collection with documents similar to such: { "_id": ObjectId("xxxxx"), "item": [ { "property": ["attr1", "+1"] }, { "property": ["attr2", "-1"] } ] } { "_id": ObjectId("xxxxy"), "item": [ { "property": ["attr1", "-1"] }, { "property": ["attr2", "0"] } ] } { "_id": ObjectId("xxxxz"), "item": [ { "property": ["attr1", "0"] }, { "property": ["attr2", "+1"] } ] } Preferably using an aggregation pipeline, is there any way to match the document if and only if any one of the properties

Projection option to return length/size of field

我的梦境 提交于 2019-12-20 04:16:31
问题 I'm simply trying to write a mongo query like in SQL: SELECT LENGTH(binaryBody) AS bodyLength FROM documents; To do so I thought I have to use the projection of the find method. db.documents.find( {<query>}, { bodyLength: {$size: 'body'}} ); But how to do that? Error: error: { "waitedMS" : NumberLong(0), "ok" : 0, "errmsg" : "Unsupported projection option: bodyLength: { $size: \"body\" }", "code" : 2 } 回答1: .find() does not "alter" documents returned in any way. You can only "include" or