aggregation-framework

How to change elements of an array field to values of a dict with a single attribute in MongoDB

落爺英雄遲暮 提交于 2021-02-11 09:10:26
问题 I need a way to change the elements of an array field to values of a dict with a single attribute. I don't have to write the result back into my table. I just have to read it that way. My table has rows like this : {a: 1, b:[ {...}, ..., {...} ], c: 2} I need a query that returns each row rewritten like this : {a: 1, b: [ {foo: { ... }}, ..., {foo: {...}} ], c: 2} In other words, each element of b becomes a dict with a single attribute, foo . This feels like a job for $project or $replaceRoot

How to modify value in array by position at aggregation framework

十年热恋 提交于 2021-02-11 04:28:13
问题 Let's say a have a simple document: { array: ["a", "b", "c", "d"] } How to modify the second value in aggregation ? With update it is very simple: db.collection.updateOne({}, { $set: { "array.1": "B" } } ) gives: { array: ["a", "B", "c", "d"] } In aggregation framework you can use this one: db.collection.aggregate([ { $set: { "array": { $map: { input: "$array", in: { $cond: { if: { $eq: [{ $indexOfArray: ["$array", "$$this"] }, 1] }, then: "B", else: "$$this" } } } } } } ]) However, this

lookup with condition in mongoose

ⅰ亾dé卋堺 提交于 2021-02-10 16:19:21
问题 I have two collections. articles and bookmarks. articles { _id: "5faa889ade5e0a6326a873d3", name: "article 1" }, { _id: "5faa889ade5e0a6326a873d", name: "article 2" } bookmarks { _id: "5faa889ade5e0a6326a873d1", user_id: "5fc7b50da483a66a86aa7e9e", model_id: "5faa889ade5e0a6326a873d3" } I want to join article with bookmark. if user bookmarked a article. what i have tried const aggregate = await Articles.aggregate([{ $lookup: { from: "categories", localField: "category_id", foreignField: "_id"

How to get last document of each day in MongoDB collection?

余生颓废 提交于 2021-02-10 14:53:46
问题 I have a model Entry to which includes details of a hospital at a particular time. The data looks like this: { "_id": "5ef9c7337874820008c1a026", "date": 1593427763640, //... some data "hospital": { "_id": "5ef8d06630c364000840bb6d", "name": "City Hospital", //... some data }, } I want to get the last query of each day grouped by the hospital ID. In MySQL, it can be achieved using INNER JOIN. How can I do it using MongoDB? 回答1: Given a day, calculate start and end of a day. This is to be used

$lookup when foreignField is array

爷,独闯天下 提交于 2021-02-10 14:41:03
问题 I have 2 collections, the first storing the animes watched by each user, their status etc: const listSchema = mongoose.Schema({ user: { type: Schema.Types.ObjectId, ref: 'user' }, animes: [{ _id: false, anime: { type: Schema.Types.ObjectId, ref: 'anime', unique: true }, status: String, episodes: Number, rating: Number }] }); and the second storing all animes and the relevant information. I want to show that second collection but adding status and episodes fields that are filled from the list

$lookup when foreignField is array

守給你的承諾、 提交于 2021-02-10 14:40:49
问题 I have 2 collections, the first storing the animes watched by each user, their status etc: const listSchema = mongoose.Schema({ user: { type: Schema.Types.ObjectId, ref: 'user' }, animes: [{ _id: false, anime: { type: Schema.Types.ObjectId, ref: 'anime', unique: true }, status: String, episodes: Number, rating: Number }] }); and the second storing all animes and the relevant information. I want to show that second collection but adding status and episodes fields that are filled from the list

MongoDB aggregate pipelines with linked object

冷暖自知 提交于 2021-02-10 05:09:44
问题 I'm linking two objects in one query and use aggregate function for it. Some data is localized and I'm using a solution from here to get data for specified locale. I am struggling to do the same with data from the linked object (rooms). Specifically, list data for given locale from the roomDetails. Please take a look at the Mongo playground 回答1: You just need to add filter in your second $addFields stage that you are filtering roomTypes and rest of the stages would be same, just highlighted

MongoDB aggregate pipelines with linked object

本秂侑毒 提交于 2021-02-10 05:09:19
问题 I'm linking two objects in one query and use aggregate function for it. Some data is localized and I'm using a solution from here to get data for specified locale. I am struggling to do the same with data from the linked object (rooms). Specifically, list data for given locale from the roomDetails. Please take a look at the Mongo playground 回答1: You just need to add filter in your second $addFields stage that you are filtering roomTypes and rest of the stages would be same, just highlighted

MongoDB aggregate pipelines with linked object

强颜欢笑 提交于 2021-02-10 05:05:51
问题 I'm linking two objects in one query and use aggregate function for it. Some data is localized and I'm using a solution from here to get data for specified locale. I am struggling to do the same with data from the linked object (rooms). Specifically, list data for given locale from the roomDetails. Please take a look at the Mongo playground 回答1: You just need to add filter in your second $addFields stage that you are filtering roomTypes and rest of the stages would be same, just highlighted

is It possible to compare two Months Data in single Collection in MongoDB?

房东的猫 提交于 2021-02-08 11:58:17
问题 I have collection database with 10 000 000 call records. I want to compare call usage of previous month to next month. Example of collection document { "_id" : ObjectId("54ed74d76c68d23af73e230a"), "msisdn" : "9818441000", "callType" : "ISD" "duration" : 10.109999656677246, "charges" : 200, "traffic" : "Voice", "Date" : ISODate("2014-01-05T19:51:01.928Z") } { "_id" : ObjectId("54ed74d76c68d23af73e230b"), "msisdn" : "9818843796", "callType" : "Local", "duration" : 1, "charges" : 150, "traffic"