mongodb-query

MongoDB: Map nested array in aggregate function

无人久伴 提交于 2020-05-31 04:00:12
问题 I am trying to update property name by using aggregation. Current document [ { _id: 5, payLoad: { grades: [ { grade_: 80, mean: 75 }, { grade_: 90, mean: 80 } ] } } ] expected document renaming grade_ to grade [ { _id: 5, payLoad: { grades: [ { grade: 80, mean: 75 }, { grade: 90, mean: 80 } ] } } ] To achieve this I am trying to map the aggregation db.collection.aggregate([ { $addFields: { payLoad: { grades: { $map: { input: "$grades", as: "grade", in: { grade: "$$grade.grade_", mean: "$

MongoDB pagination on arrays - Check if $slice reached the beginning of the array

泪湿孤枕 提交于 2020-05-31 02:24:45
问题 In my application i have an box where users can read comments. If some user scrolls down, it loads more and more comments, everything fine. I slice the array from the back to the begin because i use $push whenever somebody write some comment so i slice it like this $slice: [ -5 , 5] . This will slice the last 5 elements out of the array. The -5 is actually an variable in my aggregation pipeline. Whenever i loaded some comments i decrease the value from -5 to -10 and so on so on. The problem i

Need to calculate the datetime difference for the datetime field saved in string format in MongoDB

做~自己de王妃 提交于 2020-05-30 08:04:22
问题 Sample Documents : { "_id" : ObjectId("50ed90a55502684f440001ac"), "time" : "2020-05-14T14:12:46.058-0400", "ServerTimeStamp" : "2020-05-14T14:12:46.058-0400" } { "_id" : ObjectId("50ed90a55502684f440001ad"), "time" : "2020-05-14T15:12:46.058-0400", "ServerTimeStamp" : "2020-05-14T14:12:46.058-0400" } { "_id" : ObjectId("50ed90a55502684f440001ae"), "time" : "2020-05-14T18:12:46.058-0400", "ServerTimeStamp" :"2020-05-14T14:12:46.058-0400" } I would like to calculate the time difference of

Need to calculate the datetime difference for the datetime field saved in string format in MongoDB

梦想的初衷 提交于 2020-05-30 08:03:56
问题 Sample Documents : { "_id" : ObjectId("50ed90a55502684f440001ac"), "time" : "2020-05-14T14:12:46.058-0400", "ServerTimeStamp" : "2020-05-14T14:12:46.058-0400" } { "_id" : ObjectId("50ed90a55502684f440001ad"), "time" : "2020-05-14T15:12:46.058-0400", "ServerTimeStamp" : "2020-05-14T14:12:46.058-0400" } { "_id" : ObjectId("50ed90a55502684f440001ae"), "time" : "2020-05-14T18:12:46.058-0400", "ServerTimeStamp" :"2020-05-14T14:12:46.058-0400" } I would like to calculate the time difference of

Query MongoDB by value when parent key is unknown

有些话、适合烂在心里 提交于 2020-05-28 09:58:25
问题 I have a MongoDB collection called references that has a document where I need to return all the objects that have a key called format with the value of 1 . An example of the document looks like this: { "_id" : ObjectId("878f92ad6d9e8089aa3456a9"), "categories" : { "1" : { "format" : 1, ... } } } I have tried this: db.references.find({ "_id" : ObjectId("878f92ad6d9e8089aa3456a9"), "categories.$.format" : 1 }).pretty(); As well as this: db.references.find({ "_id" : ObjectId(

MongoDB creates array of arrays in $group $push instead of flat array

偶尔善良 提交于 2020-05-27 18:35:35
问题 I am trying to group a set of documents after an $unwind operation. My documents look like this: { "_id" : ObjectId("5cdb5b5acadf5100019da2f4"), "allowedLocations" : [ { "type" : "country", "value" : "world", "label" : "World" } ], "disallowedLocations" : [ { "type" : "country", "value" : "CF", "label" : "Central African Republic" }, { "type" : "country", "value" : "CN", "label" : "China" } ], } { "_id" : ObjectId("5cdb5b5acadf5100019da2f4"), "allowedLocations" : [ { "type" : "country",

MongoError: cannot infer query fields to set, path 'users' is matched twice

旧城冷巷雨未停 提交于 2020-05-26 11:43:51
问题 I am using mongoose. I want to create a document chat with an array users (including userId1 , userId2 ), if I do not find it: This is how I do: ChatModel.findOneAndUpdate( { users: { $all: [userId1, userId2] }}, { $setOnInsert: { users: [userId1, userId2] }}, { upsert: true }) .exec() .catch(err => console.log(err)); But I got the error: MongoError: cannot infer query fields to set, path 'users' is matched twice This is Chat Schema: { users: [{ type: Schema.Types.ObjectId, ref: 'User' }],

MongoError: cannot infer query fields to set, path 'users' is matched twice

守給你的承諾、 提交于 2020-05-26 11:43:35
问题 I am using mongoose. I want to create a document chat with an array users (including userId1 , userId2 ), if I do not find it: This is how I do: ChatModel.findOneAndUpdate( { users: { $all: [userId1, userId2] }}, { $setOnInsert: { users: [userId1, userId2] }}, { upsert: true }) .exec() .catch(err => console.log(err)); But I got the error: MongoError: cannot infer query fields to set, path 'users' is matched twice This is Chat Schema: { users: [{ type: Schema.Types.ObjectId, ref: 'User' }],

Is there an elseif thing in MongoDB to $cond while aggregating

江枫思渺然 提交于 2020-05-24 20:15:41
问题 So I need a custom field calculated in MongoDB as follows if( field1 =="A") ->customfield=10 else if(field1 =="B" )->customfield=20 else (field1 =="C" ) ->customfield=15 I'm using aggregation along with the $project statement. But the $cond operator doesn't allow elseif (subbranching the else) and merely allows two static branches if and else. Using a nested elseif causes "exception: field inclusion is not allowed inside of $expressions" Heres my query(which gives me the error) db.items

I have a mongodb query and I want to update the action to denied for Sale which meets <some random invoice number>

孤街醉人 提交于 2020-05-17 08:47:04
问题 I have a mongodb query and I want to update the action to denied for Sale which meets I want to update the action to denied. I have tried several other methods but still it is not working. Here is my json: { "Sales" : [ { "InvoiceNumber" : "Temp-ObjectID(\"5e9cb6c6ad4f2a0ef06ae159\")", "Action" : [ "Approved" ], "Log" : [ "Created on: 04/20/2020 42:04:04" ] }, { "InvoiceNumber" : "Temp-ObjectID(\"5e9cbe20287628f1cbf78fdb\")", "Action" : [ "Approve", "Decline", "Edit", "Remarks" ], "Log" : [