mongodb-query

Update multiple subdocument without exact sub document level condition

元气小坏坏 提交于 2019-12-08 04:43:24
问题 I have collection with the following document: { "_id" : ObjectId("56cbf9cd75de3ee9057b23c7"), "title" : "Abc", "comments" : [ { "_id" : "", "message" : "", "active" : 1, "status" : 1, }, { "_id" : "", "message" : "", "active" : 0, "status" : 1, }, { "_id" : "", "message" : "", "active" : 1, "status" : 1, } ], } { "_id" : ObjectId("553744ae75de3eb9128b4568"), "title" : "Jhon", "comments" : [ { "_id" : "", "message" : "", "active" : 1, "status" : 1, } ] } I need to update all status of

How to Get an Aggregate from a MongoDB Collection

▼魔方 西西 提交于 2019-12-08 04:37:52
问题 The following collection contains the ticks for the diverse exchange symbols (in this case BTCUSD and BTCEUR ): { "_id" : ObjectId("5a08d2b956df9b2302759d1a"), "symbol" : "BTCUSD", "time" : ISODate("2013-04-13T00:00:00Z"), "open" : 112, "close" : 91.1, "high" : 130, "low" : 81.12, "volume" : 23866.6770456 } { "_id" : ObjectId("5a08d2b956df9b2302759d1c"), "symbol" : "BTCUSD", "time" : ISODate("2013-04-14T00:00:00Z"), "open" : 91.1, "close" : 90.171, "high" : 109, "low" : 20, "volume" : 16437

Update Array Children Sorted Order

﹥>﹥吖頭↗ 提交于 2019-12-08 04:34:37
问题 I have a collection containing objects with the following structure { "dep_id": "some_id", "departament": "dep name", "employees": [{ "name": "emp1", "age": 31 },{ "name": "emp2", "age": 35 }] } I would like to sort and save the array of employees for the object with id "some_id", by employees.age, descending. The best outcome would be to do this atomically using mongodb's query language. Is this possible? If not, how can I rearrange the subdocuments without affecting the parent's other data

Mongodb dot field update

亡梦爱人 提交于 2019-12-08 04:26:20
问题 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

Mongodb limit array within aggregate query

北战南征 提交于 2019-12-08 04:14:22
问题 I'm attempting to write a query to return the top X terms across each category - e.g. top 5, top 10 etc. Each term has an associated category, and based up on some help from another stackoverflow question I've managed to get this: db.collection.aggregate([ { $group : { _id : { category: "$uri.category", term: "$uri.term", }, total: { $sum : 1 } } }, { $sort : { total : -1 } }, { $group : { _id : "$_id.category", terms: { $push: { term: "$_id.term", total: "$total" } } } } ]); The above query

Update multiple subdocument without exact sub document level condition

≯℡__Kan透↙ 提交于 2019-12-08 03:58:26
I have collection with the following document: { "_id" : ObjectId("56cbf9cd75de3ee9057b23c7"), "title" : "Abc", "comments" : [ { "_id" : "", "message" : "", "active" : 1, "status" : 1, }, { "_id" : "", "message" : "", "active" : 0, "status" : 1, }, { "_id" : "", "message" : "", "active" : 1, "status" : 1, } ], } { "_id" : ObjectId("553744ae75de3eb9128b4568"), "title" : "Jhon", "comments" : [ { "_id" : "", "message" : "", "active" : 1, "status" : 1, } ] } I need to update all status of comments as 0 where comments.active=1 . I tried to solve it as below, but only first record of the comments is

Sort subfields with unknown parent

我是研究僧i 提交于 2019-12-08 03:56:14
问题 I have a schema like this: { "_id" : "555", "connections" : [ { "id" : 111 "time" : 1456439249 }, { "id" : 222 "time" : 1556412345 } ... } "users" : [ "111" : { "id" : 111 "name" : "Michael" }, "222" : { "id" : 222 "name" : "Jim" } ... } I want to get sorted connections by time and users data. I'm trying with this: db.getCollection('mycollecion') .find( {'_id' : '555'}, {'_id' : 0, 'connections' : 1, 'users' : 1} ).sort({'connections.time' : 1}) Probably "connections.time" is not the correct

Retrieve Relevance Ordered Result from Text Query on MongoDB Collection using the C# Driver

守給你的承諾、 提交于 2019-12-08 03:13:41
问题 I am attempting to text query a collection and retrieve the results in the text match order. The docs explain pretty well how to do this in the shell: db.articles.find( { status: "A", $text: { $search: "coffee cake" } }, { score: { $meta: "textScore" } } ).sort( { date: 1, score: { $meta: "textScore" } } ) but it requires the projection of the additional score field from the find into the sort. In C#, I have a function that looks like this: public IEnumerable<T> TextSearch<T>(MongoCollection

MongoDB - Aggregate muliple rows

我怕爱的太早我们不能终老 提交于 2019-12-08 03:06:17
问题 Suppose the following aggregation query: Machine.aggregate( [ { $match : { $and: [ {"idc": req.query.idc }, {"customer":req.query.customer} ] } } ,{"$group":{_id: {"cluster":"$cluster","idc":"$idc","type":"$type"},"SumCores":{"$sum":"$cores"},"SumMemory": { "$sum":"$memory" }}}, { $sort : { idc : -1, cluster: 1 } } ]); Which returns: [ { "_id": { "cluster": 1, "idc": "LH5", "type": "Virtual" }, "SumCores": 112, "SumMemory": 384 }, { "_id": { "cluster": 1, "idc": "LH5", "type": "Physical" },

User input for a MongoDB search

百般思念 提交于 2019-12-08 02:45:01
问题 I am currently trying to accept user input so that a user may be able to search the database. > db.test2.find().pretty() { "_id" : ObjectId("55de8a17f8389e208a1e7d7e"), "name" : "john", "favorites" : { "vegetable" : "spinach", "fruit" : "apple", } } { "_id" : ObjectId("55de8a17f8389e208a1f6gg4"), "name" : "becky", "favorites" : { "vegetable" : "spinach", "fruit" : "apple", } } { "_id" : ObjectId("55e3b6cbec2740181355b809"), "name" : "liz", "favorites" : { "vegetable" : "spinach", "fruit" :