mongodb-query

Concurrent update of array elements which are embedded documents in MongoDB

早过忘川 提交于 2019-12-11 12:19:29
问题 I have documents like this one at collection x at MongoDB: { "_id" : ... "attrs" : [ { "key": "A1", "type" : "T1", "value" : "13" }, { "key": "A2", "type" : "T2", "value" : "14" } ] } The A1 and A2 elements above are just examples: the attrs field may hold any number of array elements. I'd need to access concurrently to the attrs array from several independent clients accessing to MongoDB. For example, considers two clients, one wanting to change the value of the element identified by key

MongoDB Covered Query For Two Fields Without Compound Index

别等时光非礼了梦想. 提交于 2019-12-11 11:46:16
问题 Can you perform a MongoDB covered query for two fields, for example db.collection.find( { _id: 1, a: 2 } ) without having a compound index such as db.collection.ensureIndex( { _id: 1, a: 1 } ) but instead having only one index for _id (you get that by default) and another index for field "a", as in db.collection.ensureIndex( { a: 1 } ) In other words, I'd like to know if in order to perform a covered query for two fields I need a compound index vs. needing only two single (i.e., not compound)

Updating array with push and slice

橙三吉。 提交于 2019-12-11 11:35:02
问题 I have just started to play with MongoDB and have some questions about how I update my documents in the database. I insert two documents in my db with db.userscores.insert({name: 'John Doe', email: 'john.doe@mail.com', levels : [{level: 1, hiscores: [90, 40, 25], achivements: ['capture the flag', 'it can only be one', 'apple collector', 'level complete']}, {level: 2, hiscores: [30, 25], achivements: ['level complete']}, {level: 3, hiscores: [], achivements: []}]}); db.userscores.insert({name:

Match on key from two queries in a single query

陌路散爱 提交于 2019-12-11 11:34:15
问题 I have time series data in mongodb as follows: { "_id" : ObjectId("558912b845cea070a982d894"), "code" : "ZL0KOP", "time" : NumberLong("1420128024000"), "direction" : "10", "siteId" : "0000" } { "_id" : ObjectId("558912b845cea070a982d895"), "code" : "AQ0ZSQ", "time" : NumberLong("1420128025000"), "direction" : "10", "siteId" : "0000" } { "_id" : ObjectId("558912b845cea070a982d896"), "code" : "AQ0ZSQ", "time" : NumberLong("1420128003000"), "direction" : "10", "siteId" : "0000" } { "_id" :

Aggregating a collection in mongo to determine counts

烈酒焚心 提交于 2019-12-11 11:19:34
问题 I have a collection in MongoDB that contains several fields, and is written to by my application server every time a user performs an action. A sort of log-collection. Each document resembles the following pattern, omitting a few irrelevant fields for the sake of brevity: { "_id" : ObjectId("5536727686e09bab576f1c94"), "_user" : ObjectId("5536597b60b2321aadeb8a7c"), "_userAgent" : "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0", "_systemTime" : ISODate("2015-04

Mongo incorrectly sorting on array field?

不问归期 提交于 2019-12-11 11:19:08
问题 I'm trying to filter on an array field which unfortunately doesn't seem to be working correctly. Everything I've read suggests that this should work, and the sort is doing something , just not what I expected and I can't explain it. What I'm trying to achieve is sorting on an array sub-field. I've managed to achieve most of this using the positional operator but I can't work out what the sort is doing. db.getCollection('boards') .find({ "lastVisited.user": "AAA" }, { name: 1, "lastVisited.$"

MongoDB aggregate() - error “TypeError: Cannot call method 'forEach' of undefined”

假装没事ソ 提交于 2019-12-11 10:45:33
问题 I have the following scrip in "script.js" conn = new Mongo(); db = conn.getDB("learn"); db.contracts.aggregate([ { $match: { regionCode: '77' } }, { $unwind: '$products' }, { $project: { _id: '$_id', regNum: '$regNum', prodName: '$products.name', prodPrice: '$products.price' } }, { $match: { 'prodName' : 'Water' } } ], {cursor:{}}).result.forEach(printjson); I run it from the command prompt by the following way mongo script.js >> out.txt In file "out.txt" I have the error TypeError: Cannot

Find min and max value from the array in mongodb

限于喜欢 提交于 2019-12-11 10:25:41
问题 I have Following Project Collection Project Collection : [ { Id : 1, name : p1, tasks : [{ taskId : t1, startDate : ISODate("2018-09-24T10:02:49.403Z"), endDate : ISODate("2018-09-26T10:02:49.403Z"), }, { taskId : t2, startDate : ISODate("2018-09-24T10:02:49.403Z"), endDate : ISODate("2018-09-29T10:02:49.403Z"), }, { taskId : t3, startDate : ISODate("2018-09-24T10:02:49.403Z"), endDate : ISODate("2018-09-27T10:02:49.403Z"), }] } ] How to get p1 project's startDate and EndDate depending on

MongoDB sort vs aggregate $sort on array index

∥☆過路亽.° 提交于 2019-12-11 10:18:00
问题 With a MongoDB collection test containing the following documents: { "_id" : 1, "color" : "blue", "items" : [ 1, 2, 0 ] } { "_id" : 2, "color" : "red", "items" : [ 0, 3, 4 ] } if I sort them in reversed order based on the second element in the items array, using db.test.find().sort({"items.1": -1}) they will be correctly sorted as: { "_id" : 2, "color" : "red", "items" : [ 0, 3, 4 ] } { "_id" : 1, "color" : "blue", "items" : [ 1, 2, 0 ] } However, when I attempt to sort them using the

mongotemplate aggregation with unique results

末鹿安然 提交于 2019-12-11 10:16:19
问题 I am using mongotemplate , my collection look like this: { "_id": "theid", "tag": "taga", "somefield": "some value", "fieldC": { "anotherfielad": "more data", "an_array": [ { "a": "abc", "b": 5 }, { "a": "bca", "b": 44 }, { "a": "ddd", "b": 21 } ] } } { "_id": "anotherid", "tag": "taga", "somefield": "some other value", "fieldC": { "anotherfielad": "other more data", "an_array": [ { "a": "ccc", "b": 6 }, { "a": "abc", "b": 99 }, { "a": "ddd", "b": 21 } ] } } I need to get a unique result from