mongodb-query

Select last value of array in sub document

本秂侑毒 提交于 2020-05-15 05:07:30
问题 I have the following document structure { "_id" : 1, "prices" : { "100" : [ 5.67, . . 1.7, ], "101" : [ 4.67, . . 1.4, }, "v" : 2 } We need to get the last value from each of the fields "100" and "101", e.g 1.7 & 1.4, using mongodb v3.0.2. Currently we are loading the whole document and then getting the required values in the application. It has become quite a bottleneck as each document may be ~1MB, which for 100s of documents is adding up. We are hoping there is a suitable feature of

Mongodb if then condition under filter how to make

余生长醉 提交于 2020-05-14 14:18:50
问题 db.main.aggregate([ { $lookup: { from: "history", localField: "history_id", foreignField: "history_id", as: "History" } }, { $project: { "History": { $filter: { input: "$History", as: "his", if: { $eq: [ "5e4a8d2d3952132a08ae5764", "$$his.user_id" ] }, then: { cond: { $and: [ { $lt: [ "$$his.date", "$date" ] }, { $eq: [ "5e4a8d2d3952132a08ae5764", "$$his.user_id" ] } ] } }, else: {} } }, data: 1, history_id: 1, sender_id: 1, text: 1, date: 1 } }, { $unwind: "$History" } ]) MongoPlayground

Filter nested array with conditions based on multi-level object values and update them - MongoDB aggregate + update

二次信任 提交于 2020-05-14 02:27:05
问题 Considering I have the following documents in a collection (ignoring the _id ) : [ { "Id": "OP01", "Sessions": [ { "Id": "Session01", "Conversations": [ { "Id": "Conversation01", "Messages": [ { "Id": "Message01", "Status": "read", "Direction": "inbound" }, { "Id": "Message02", "Status": "delivered", "Direction": "internal" }, { "Id": "Message03", "Status": "delivered", "Direction": "inbound" }, { "Id": "Message04", "Status": "sent", "Direction": "outbound" } ] }, { "Id": "Conversation02",

What is difference between “size” and “storageSize” displayed by Mongo stats() function

妖精的绣舞 提交于 2020-05-12 14:40:55
问题 What is difference between "size" and "storageSize" displayed by Mongo stats() function? Which one shows the actual size on disk for a particular collection? Refer sample data returned by command db.getCollection('temp_collection').stats() {"ns" : "DB1.temp_collection", "count" : 1035219, "size" : 1186, "avgObjSize" : 1202, "storageSize" : 177, "capped" : false, "wiredTiger" : } 回答1: in simple words: size refers for logical allocation (this is seen by database engine) storageSize refers for

What is difference between “size” and “storageSize” displayed by Mongo stats() function

白昼怎懂夜的黑 提交于 2020-05-12 14:40:43
问题 What is difference between "size" and "storageSize" displayed by Mongo stats() function? Which one shows the actual size on disk for a particular collection? Refer sample data returned by command db.getCollection('temp_collection').stats() {"ns" : "DB1.temp_collection", "count" : 1035219, "size" : 1186, "avgObjSize" : 1202, "storageSize" : 177, "capped" : false, "wiredTiger" : } 回答1: in simple words: size refers for logical allocation (this is seen by database engine) storageSize refers for

What is difference between “size” and “storageSize” displayed by Mongo stats() function

旧巷老猫 提交于 2020-05-12 14:39:27
问题 What is difference between "size" and "storageSize" displayed by Mongo stats() function? Which one shows the actual size on disk for a particular collection? Refer sample data returned by command db.getCollection('temp_collection').stats() {"ns" : "DB1.temp_collection", "count" : 1035219, "size" : 1186, "avgObjSize" : 1202, "storageSize" : 177, "capped" : false, "wiredTiger" : } 回答1: in simple words: size refers for logical allocation (this is seen by database engine) storageSize refers for

What is difference between “size” and “storageSize” displayed by Mongo stats() function

孤人 提交于 2020-05-12 14:39:14
问题 What is difference between "size" and "storageSize" displayed by Mongo stats() function? Which one shows the actual size on disk for a particular collection? Refer sample data returned by command db.getCollection('temp_collection').stats() {"ns" : "DB1.temp_collection", "count" : 1035219, "size" : 1186, "avgObjSize" : 1202, "storageSize" : 177, "capped" : false, "wiredTiger" : } 回答1: in simple words: size refers for logical allocation (this is seen by database engine) storageSize refers for

auto increment ids in mongoose

泪湿孤枕 提交于 2020-05-12 04:58:24
问题 How do I have autoincrement ids in mongoose? I want my ids to start like 1, 2, 3, 4, not the weird id numbers mongodb creates for you? Here's my schema: var PortfolioSchema = mongoose.Schema({ url: String, createTime: { type: Date, default: Date.now }, updateTime: { type: Date, default: Date.now }, user: {type: Schema.Types.ObjectId, ref: 'User'} }); 回答1: Use mongoose-auto-increment: https://github.com/codetunnel/mongoose-auto-increment var mongoose = require('mongoose'); var autoIncrement =

auto increment ids in mongoose

我的梦境 提交于 2020-05-12 04:56:13
问题 How do I have autoincrement ids in mongoose? I want my ids to start like 1, 2, 3, 4, not the weird id numbers mongodb creates for you? Here's my schema: var PortfolioSchema = mongoose.Schema({ url: String, createTime: { type: Date, default: Date.now }, updateTime: { type: Date, default: Date.now }, user: {type: Schema.Types.ObjectId, ref: 'User'} }); 回答1: Use mongoose-auto-increment: https://github.com/codetunnel/mongoose-auto-increment var mongoose = require('mongoose'); var autoIncrement =

The field “$name” must be an accumulator object

醉酒当歌 提交于 2020-05-10 10:14:19
问题 I have a query and when I use a $group a error shows "the field "$name must be an accumulator object", if if remove the filed "$name" all works well and i have tried to use only "name" instead of "$name" and the error continues. User.aggregate([ { $match: { "storeKey": req.body.store } }, { $group: { "_id": "$_id", "name": "$name", "count": { "$sum": 1 }, "totalValue": { "$sum": "$value" } } }, { $sort: sort }, { $skip: req.body.limit * req.body.page }, { $limit: req.body.limit } ])... 回答1: