mongodb-query

Mongoose: how to use index in aggregate?

岁酱吖の 提交于 2020-02-02 16:14:32
问题 How can I use indexes in aggregate? I saw the document https://docs.mongodb.com/manual/core/aggregation-pipeline/#pipeline-operators-and-indexes The $match and $sort pipeline operators can take advantage of an index when they occur at the beginning of the pipeline. Is there any way of using index not the beginning situation? like $sort , $match or $group Please help me 回答1: An index works by keeping a record of certain pieces of data that point to a given record in your collection. Think of

Merge two objects in mongoose

一世执手 提交于 2020-02-02 06:33:26
问题 I have two models in user.js and userProfile.js of mongoose, where I want to get documents using a join query but I have not take ref in user schema so I have code as below: user.js var userSchema = new Schema({ nick_name:{type:String}, email: {type: String}, password: {type: String}, is_active:{type:String,enum:['1','0'],default:"1"}, },{ collection: 'user'}); userProfile.js var userProfileSchema = new Schema({ user_id:{type:Schema.Types.ObjectId, ref:'User'}, first_name:{type:String}, last

Merge two objects in mongoose

自作多情 提交于 2020-02-02 06:32:31
问题 I have two models in user.js and userProfile.js of mongoose, where I want to get documents using a join query but I have not take ref in user schema so I have code as below: user.js var userSchema = new Schema({ nick_name:{type:String}, email: {type: String}, password: {type: String}, is_active:{type:String,enum:['1','0'],default:"1"}, },{ collection: 'user'}); userProfile.js var userProfileSchema = new Schema({ user_id:{type:Schema.Types.ObjectId, ref:'User'}, first_name:{type:String}, last

MongoDB Find performance: single compound index VS two single field indexes

对着背影说爱祢 提交于 2020-01-30 15:30:08
问题 I'm looking for an advice about which indexing strategy to use in MongoDb 3.4. Let's suppose we have a people collection of documents with the following shape: { _id: 10, name: "Bob", age: 32, profession: "Hacker" } Let's imagine that a web api to query the collection is exposed and that the only possibile filters are by name or by age . A sample call to the api will be something like: http://myAwesomeWebSite/people?name="Bob"&age=25 Such a call will be translated in the following query: db

MongoDB Find performance: single compound index VS two single field indexes

独自空忆成欢 提交于 2020-01-30 15:27:50
问题 I'm looking for an advice about which indexing strategy to use in MongoDb 3.4. Let's suppose we have a people collection of documents with the following shape: { _id: 10, name: "Bob", age: 32, profession: "Hacker" } Let's imagine that a web api to query the collection is exposed and that the only possibile filters are by name or by age . A sample call to the api will be something like: http://myAwesomeWebSite/people?name="Bob"&age=25 Such a call will be translated in the following query: db

How to update multiple fields of an array object with one request?

我只是一个虾纸丫 提交于 2020-01-30 09:07:10
问题 { _id:xxxxstoreid store:{ products:[ { _id:xxxproductid, name:xxx, img:url, } ] } } since i cannot predict the request for update, params may have just name or it may have both. here is my query,it updates successfully but it removes other fields if they are not present in the params. eg: var params={_id:xxid,name:'xxx',img:'xxx'} or var params={_id:xxid,name:'xxx'} in this case if params have just name it removes img field and updates. User.update({'store.products._id':params._id},{$set:{

How to update multiple fields of an array object with one request?

北城以北 提交于 2020-01-30 09:06:15
问题 { _id:xxxxstoreid store:{ products:[ { _id:xxxproductid, name:xxx, img:url, } ] } } since i cannot predict the request for update, params may have just name or it may have both. here is my query,it updates successfully but it removes other fields if they are not present in the params. eg: var params={_id:xxid,name:'xxx',img:'xxx'} or var params={_id:xxid,name:'xxx'} in this case if params have just name it removes img field and updates. User.update({'store.products._id':params._id},{$set:{

How to update multiple fields of an array object with one request?

六眼飞鱼酱① 提交于 2020-01-30 09:05:11
问题 { _id:xxxxstoreid store:{ products:[ { _id:xxxproductid, name:xxx, img:url, } ] } } since i cannot predict the request for update, params may have just name or it may have both. here is my query,it updates successfully but it removes other fields if they are not present in the params. eg: var params={_id:xxid,name:'xxx',img:'xxx'} or var params={_id:xxid,name:'xxx'} in this case if params have just name it removes img field and updates. User.update({'store.products._id':params._id},{$set:{

$replaceRoot in mongodb aggregation

我是研究僧i 提交于 2020-01-30 08:10:13
问题 I have a collection like this: { "_id" : ObjectId("5bd1686ba64b9206349284db"), "type" : "Package", "codeInstances" : [ { "name" : "a", "description" : "a" }, { "name" : "b", "description" : "b1" }, { "name" : "b", "description" : "b2" } ] } { "_id" : ObjectId("5bd16ab8a64b92068d485054"), "type" : "Package", "codeInstances" : [ { "name" : "a", "description" : "a" }, { "name" : "b", "description" : "b3" } ] } The following structure is what I want: { "name" : "b", "description" : "b1" }, {

$nin with the $expr

微笑、不失礼 提交于 2020-01-30 05:16:15
问题 I have a query to find if a user CreatedBy is in a SharedWith . I want to inverse the query to check if the CreatedBy is not in SharedWith . [ { "$match": { "$and": [ { "$and": [ { "SharedWith": { "$exists": true } }, { "$expr": { "$in": [ "$CreatedBy", "$Multi_User" ] } } ] } ] } } ] MongoDB does not support a direct $nin or $not for $and query. Any idea how to achieve this. The user document looks like this, Collection = [ {"CreatedBy": {"_id": "User001", "Email": "user@eg.com", "Name":