aggregation-framework

Update MongoDB field using value of another field

不羁岁月 提交于 2021-02-05 11:12:32
问题 In MongoDB, is it possible to update the value of a field using the value from another field? The equivalent SQL would be something like: UPDATE Person SET Name = FirstName + ' ' + LastName And the MongoDB pseudo-code would be: db.person.update( {}, { $set : { name : firstName + ' ' + lastName } ); 回答1: The best way to do this is in version 4.2+ which allows using of aggregation pipeline in the update document and the updateOne, updateMany or update collection method. Note that the latter has

Update MongoDB field using value of another field

﹥>﹥吖頭↗ 提交于 2021-02-05 11:10:32
问题 In MongoDB, is it possible to update the value of a field using the value from another field? The equivalent SQL would be something like: UPDATE Person SET Name = FirstName + ' ' + LastName And the MongoDB pseudo-code would be: db.person.update( {}, { $set : { name : firstName + ' ' + lastName } ); 回答1: The best way to do this is in version 4.2+ which allows using of aggregation pipeline in the update document and the updateOne, updateMany or update collection method. Note that the latter has

How to group arrays based on another array in mongodb aggregation?

给你一囗甜甜゛ 提交于 2021-02-05 09:42:38
问题 I have seen kind of a similar question, but it has no answer so it doesn't answer my question. How to group arrays based on another array in mongodb aggregation? Assume you have these schemas: PollSchema const PollSchema = new mongoose.Schema({ numberOfChoices: { type: Number, required: true, min: [1, "Number of choices must be at least 1"], max: [10, "Number of choices cannot be more than 10"], }, arrayOfNamesOfChoices:[{ name: { type: String, required: true, }, }], }); CastedVotedSchema

MongoDB Project Array Document Size with Condition

微笑、不失礼 提交于 2021-02-05 08:36:05
问题 Let's say I have the following document structure: { "_id": "ID", "array": [ { "a": "A", "b": "B", "c": { "x": true, "y": true, "z": false } }, { "a": "A", "b": "B" }, { "a": "A", "b": "B" "c": { "s": true } } ] } I am trying to do an aggregation which gives me this type of output: { "_id": "ID", "array": [ { "a": "A", "b": "B", "c": 2 }, { "a": "A", "b": "B", "c": 0 }, { "a": "A", "b": "B" "c": 1 } ] } So what I want to do is to, instead of the document c , return the number of elements with

MongoDB filter objects array content based on object member

自闭症网瘾萝莉.ら 提交于 2021-02-05 08:15:27
问题 I have the following objects array and was wondering if there is a way to filter results as return only QtyIn records or return only QtyOut records? Any hint is highly appreciated. Thanks for your help { warehouseID: "1234", transactions : [ { "qtyIn" : "10", "transDateTime" : ISODate("2019-09-10T18:54:41.983Z") }, { "qtyOut" : "11", "transDateTime" : ISODate("2019-08-10T18:54:41.983Z") }, { "qtyOut" : "200", "transDateTime" : ISODate("2019-02-10T11:54:41.983Z") } ], } 回答1: You can compare

Group and count using aggregation framework

寵の児 提交于 2021-02-05 07:48:07
问题 I'm trying to group and count the following structure: [{ "_id" : ObjectId("5479c4793815a1f417f537a0"), "status" : "canceled", "date" : ISODate("2014-11-29T00:00:00.000Z"), "offset" : 30, "devices" : [ { "name" : "Mouse", "cost" : 150, }, { "name" : "Keyboard", "cost" : 200, } ], }, { "_id" : ObjectId("5479c4793815a1f417d557a0"), "status" : "done", "date" : ISODate("2014-10-20T00:00:00.000Z"), "offset" : 30, "devices" : [ { "name" : "LCD", "cost" : 150, }, { "name" : "Keyboard", "cost" : 200,

Aggregate and sum by one key and rest

谁说胖子不能爱 提交于 2021-02-04 08:27:36
问题 It's hard for me to explain what I want to get, so I will show an example: I have objects: {name: 'steve', received: 100} {name: 'carolina', received: 70} {name: 'steve', 'received: 30} {name: 'andrew', received: 10} I can do: { $group : { _id : '$name', sum : { "$sum" :'$received' }, }, }, And i will get: Steve received 130 (100 +30) Carolina received 70 Andrew received 10 But I need something like that: Steve received 130 (100 +30) Everyone else received 80 (70+10) How can I get this effect

Group records by month and count them - Mongoose, nodeJs, mongoDb

二次信任 提交于 2021-02-02 10:00:25
问题 I need to query in database (mongoose) and get back the number of sales made each month of the year for one particular product (within one year period). I am new to node and mongoDb and I have come with a 'dummy' solution where I query in database and get back all the results for one product and than I use 3 loops to split the results in months but I think that it uses more resources than it should and it will use even more if it gets filled with more data, so I need help making a database

Group records by month and count them - Mongoose, nodeJs, mongoDb

扶醉桌前 提交于 2021-02-02 09:57:44
问题 I need to query in database (mongoose) and get back the number of sales made each month of the year for one particular product (within one year period). I am new to node and mongoDb and I have come with a 'dummy' solution where I query in database and get back all the results for one product and than I use 3 loops to split the results in months but I think that it uses more resources than it should and it will use even more if it gets filled with more data, so I need help making a database

Unrecognized expression '$first'

谁都会走 提交于 2021-01-29 21:50:53
问题 I need to extract the first item from an array and add it to its own object, so I found $first the does exactly that https://docs.mongodb.com/manual/reference/operator/aggregation/first-array-element/#example However, I was getting the error Unrecognized expression '$first' and thought it was my query that wasn't working, so I cloned the exact example they have in their docs, but it gives me the same error. db.runninglog.insertMany([ { "_id" : 1, "team" : "Anteater", log: [ { run: 1, distance