mongodb-query

How create a Date field with default value as the current timestamp in MongoDb?

天大地大妈咪最大 提交于 2019-12-06 19:22:00
问题 How to create a date field with default value,the default value should be current timestamps whenever the insertion happened in the collection. 回答1: Thats pretty simple! When you're using Mongoose for example, you can pass functions as a default value. Mongoose then calls the function for every insertion. So in your Schema you would do something like: { timestamp: { type: Date, default: Date.now}, ... } Remember to only pass the function object itself Date.now and not the value of the

Mongo aggregation cursor & counting

牧云@^-^@ 提交于 2019-12-06 18:39:14
问题 According to the mongodb node driver docs the aggregate function now returns a cursor (from 2.6). I hoped that I could use this to get a count of items pre limit & skipping but there doesn't seem to be any count function on the created cursor. If I run the same queries in the mongo shell the cursor has an itcount function that I can call to get what I want. I saw that the created cursor has an on data event (does that mean it's a CursorStream?) which seemed to get triggered the expected

add fields where condition match to nested array

▼魔方 西西 提交于 2019-12-06 17:05:51
问题 I have following users collection [{ "_id" : ObjectId("5afadfdf08a7aa6f1a27d986"), "firstName" : "bruce", "friends" : [ ObjectId("5afd1c42af18d985a06ac306"),ObjectId("5afd257daf18d985a06ac6ac") ] }, { "_id" : ObjectId("5afbfe21daf4b13ddde07dbe"), "firstName" : "clerk", "friends" : [], }] and have friends collection [{ "_id" : ObjectId("5afd1c42af18d985a06ac306"), "recipient" : ObjectId("5afaab572c4ec049aeb0bcba"), "requester" : ObjectId("5afadfdf08a7aa6f1a27d986"), "status" : 2, }, { "_id" :

Grouping documents in pairs using mongo aggregation

你。 提交于 2019-12-06 15:57:45
I have a collection of items, [ a, b, c, d ] And I want to group them in pairs such as, [ [ a, b ], [ b, c ], [ c, d ] ] This will be used in calculating the differences between each item in the original collection, but that part is solved using several techniques such as the one in this question . I know that this is possible with map reduce, but I want to know if it's possible with aggregation. Edit: Here's an example, The collection of items; each item is an actual document. [ { val: 1 }, { val: 3 }, { val: 6 }, { val: 10 }, ] Grouped version: [ [ { val: 1 }, { val: 3 } ], [ { val: 3 }, {

MongoDB/Mongoose - Querying an array of objects by date

回眸只為那壹抹淺笑 提交于 2019-12-06 15:50:19
I have the following metrics collection: { name: "Hello", values: [ { value: 2629, date: "2016-10-28T07:00:00.000Z", _id: "58453abfef7aaa15ac1fdee8" }, { value: 1568, date: "2016-10-29T07:00:00.000Z", _id: "58453abfef7aaa15ac1fdee7" }, { value: 1547, date: "2016-10-30T07:00:00.000Z", _id: "58453abfef7aaa15ac1fdee6" }, { value: 1497, date: "2016-10-31T07:00:00.000Z", _id: "58453abfef7aaa15ac1fdee5" }, { value: 3031, date: "2016-11-01T07:00:00.000Z", _id: "58453abfef7aaa15ac1fdee4" }, { value: 2559, date: "2016-11-02T07:00:00.000Z", _id: "58453abfef7aaa15ac1fdee3" }, { value: 2341, date: "2016

How to get this result with aggregate in mongoDB

风流意气都作罢 提交于 2019-12-06 15:45:08
I have an invoice collection... { "_id" : 1, "items" : [ { "_id" : 1, "value" : 100, "price" : 9500 }, { "_id" : 2, "value" : 200, "price" : 9500 } ], "costs" : [ { "_id" : 1, "price" : 100 }, { "_id" : 2, "price" : 150 }, { "_id" : 3, "price" : 250 } ]} I get Invoice amount with aggregate... In other words => {$sum : {$multiply :{[$items.value, $items.price]} }} - {$sum : "$costs.price"}; { "_id" : 1, "amount" : 2849500 } very thinks... ;-) Mongo 3.4 Version $map to multiply the item's price & value and $reduce to calculate the sum of item's price & value and $reduce to calculate the sum of

MongoDb Select Query Issue Whit Regular Expression (Starts whit and Ends Whit)

99封情书 提交于 2019-12-06 15:37:10
I need a regular expression to validate a String like this: 1678;1678;1678;1678 and 1;0;1;1;0;1 I tried to use this pattern: db.getCollection('CollectionName').find( { "magnitude": /^[1678][1678]$/, "flag": /^[1][1]$/ } ) but it doesnt works, i try this two patterns that works separate but not both at the same time db.getCollection('CollectionName').find( { "magnitude": /[1678]$/, "flag": /[1]$/ } ) db.getCollection('CollectionName').find( { "magnitude": /^[1678]/, "flag": /^[1]/ } ) I didnt find any character like * in SQL to use in this I am using robomongo 1.0.0 for querys I will apreciate

How to convert below MongoDB query into PHP?

丶灬走出姿态 提交于 2019-12-06 15:24:21
问题 Below query worked fine in Studio 3T and Robomongo, But I want to convert it into PHP format, Following is my query, db.news.aggregate([ { $match: { "_id" : "1" } }, { "$project": { "comments": { "$filter": { "input": "$comments", "as": "comment", "cond": { "$eq": [ "$$comment.status", "active" ] } } } } }, { "$project": { "comments": { "$slice": [ { "$slice": [ "$comments", { "$subtract": [ { "$size": [ "$comments" ] }, 1 ] } ] }, -1 ] } } }]) I have tried below, But it giving error " Error:

Insert Into Array of object MongoDB

家住魔仙堡 提交于 2019-12-06 15:14:50
I have { "_id" : ObjectId("5a25af80d2c4dd065991dccc"), "username" : "abc@gmail.com", "password" : "$2a$10$ptFyLKtyyrL5gMYdXS6wV.HLUA4Py5iudMJDldf5qsHFS4.9TPCyy", "role" : "Admin", "__v" : 0, "list" : [ { "name" : "We", "arr" : [ "5a26d554677475818a795f75", "5a395bb0976d5304c07f7dd4" ] }, { "name" : "sandeep", "arr" : [ "5a26d554677475818a795f75" ] } ] } I want to add an element inside list.arr where name = 'we' and add only if the element does not exist how do i perform this query. if i properly understood you question,you want to match name field with we key,and update arr only if it exists ?

Mongodb dot field update

走远了吗. 提交于 2019-12-06 14:58:37
I have the following Schema in Mongo: var OrderSchema = new Schema({ postcode: String, ... status: { last_check: { type: Date }, date: Date, code: String, postnum: String, text: String, class: String }, }); I have a function to save a data (old way is commented): function save_order(data) { // var order = new Order(data); // var upsertData = order.toObject(); // delete upsertData._id; // Order.findOneAndUpdate({postcode: order.postcode}, upsertData, {upsert: true}, function (err) { Order.update({postcode: data.postcode}, {$set:data}, function (err) { if (err) console.log('err'); }); } Here