mongodb-query

Mongo Query Nested Field Values with two-level unknown parent keys

随声附和 提交于 2019-12-10 18:12:32
问题 We use MongoDB to store time-series sensor data similar to the schema designed shown in https://www.mongodb.com/blog/post/schema-design-for-time-series-data-in-mongodb We do get good performance on data queries through time periods. Explanation on the our schema design: "v" is the parent key of sensor readings, the time is converted into nested array using Minutes and Seconds. We use "m" (Minute) as sub-parent key, then "s" (Second) as sub-key of minute reading. The sensor readings are

Find element based on two values

风格不统一 提交于 2019-12-10 18:11:33
问题 I'm trying to do a simple find on my test collection Here's an entry sample: { "_id": "movie:1", "title": "Vertigo", "year": 1958, "genre": "drama", "summary": "Scottie Ferguson, ancien inspecteur de police, est sujet au vertige depuis qu'il a vu mourir son collègue. Elster, son ami, le charge de surveiller sa femme, Madeleine, ayant des tendances suicidaires. Amoureux de la jeune femme Scottie ne remarque pas le piège qui se trame autour de lui et dont il va être la victime... ", "country":

$addToSet Based on Object key exists

末鹿安然 提交于 2019-12-10 17:49:30
问题 I have array 'pets': [{'fido': ['abc']} that is a embeded document. When I add a pet to the array, how can I check to see if that pet already exists? For instance, if I added fido again... how can I check if only fido exists and not add it? I was hoping I could use $addToSet but I only want to check part of the set(the pets name). User.prototype.updatePetArray = function(userId, petName) { userId = { _id: ObjectId(userId) }; return this.collection.findOneAndUpdate(userId, { $addToSet: { pets:

MongoDB: Querying multiple collections with two queries?

房东的猫 提交于 2019-12-10 17:45:22
问题 I’ve two collections posts and authors . A post document contains, beside the post data, a DBref link ID to an author _id . My collection looks like this: posts "_id" : ObjectId("4fa12443d3269e98070013b4"), "author" : { "$ref" : "authors", "$id" : ObjectId("4fa1242bd3269e9807000023") }, "post" : " mi eleifend egestas. Sed pharetra, felis eget varius ultrices, mauris ipsum porta elit, a feugiat tellus lorem eu metus. In lorem.", "post_title" : "Volutpat. Nulla facilisis. Suspendisse commodo

Update embedded document in Mongodb: Performance issue?

会有一股神秘感。 提交于 2019-12-10 17:43:12
问题 I am new to Mongodb and I heard that Mongodb is good for massive amount of read and write operations. Embedded document is one of the features that make it happen. But I am not sure if it is also a cause of performance issue. Book document example: { "_id": 1, "Authors": [ { "Email": "email", "Name": "name" } ], "Title": "title", ... } If there are thousands of books by one author, and his email needs to be updated, I need to write some query which can search through all book documents, pick

How to Update Multiple Array Elements in mongodb

六月ゝ 毕业季﹏ 提交于 2019-12-10 17:35:41
问题 I have a Mongo document which holds an array of elements. I'd like to reset the .handled attribute of all objects in the array where .profile = XX. The document is in the following form: { "_id": ObjectId("4d2d8deff4e6c1d71fc29a07"), "user_id": "714638ba-2e08-2168-2b99-00002f3d43c0", "events": [{ "handled": 1, "profile": 10, "data": "....." } { "handled": 1, "profile": 10, "data": "....." } { "handled": 1, "profile": 20, "data": "....." } ... ] } so, I tried the following: .update({"events

Get documents with nested objects matching count condition

风格不统一 提交于 2019-12-10 17:08:04
问题 I am a mongo noob and am working with a mongo collection with records that look like so: { "cats" [ { "name": "fluffy", "color": "red", }, { "name": "snowball", "color": "white", }, ] { I would like to perform a query that gets all records that have more than 1 white cats. MapReduce looks promising, but seems like overkill. Any help is appreciated. 回答1: You can use the aggregation framework to do this. You don't need to use the $where operator. db.collection.aggregate([ { "$match": { "cats

How to filter array of sub-documents?

牧云@^-^@ 提交于 2019-12-10 16:36:19
问题 I have a document structured like this: { "_id" : ObjectId("564d2702d4c68225cb00726f"), "list" : [ { "a" : NumberInt(1), "test" : "public" }, { "a" : NumberInt(2), "test" : "public" }, { "a" : NumberInt(3), "test" : "public" }, { "a" : NumberInt(4), "test" : "public" }, { "a" : NumberInt(5), "test" : "public" } ], "other_fields": ""} Can I filter subdocument for a in (1, 5) My expect result below { "_id" : ObjectId("564d2702d4c68225cb00726f"), "list" : [ { "a" : NumberInt(1), "test" : "public

Is it possible to compare string with ObjectId via $lookup

瘦欲@ 提交于 2019-12-10 16:35:26
问题 table1 has a field string "value" and table2 has a field "value" as ObjectId , Is it possible to do a query like this or how to write table1.aggregate([ { $lookup: { from: "table2", localField: "value", foreignField: "_id", as: "test" } } ]) 回答1: As far I know to join collections using $lookup operator in MongoDB data type should be same. If type mismatch then $lookup will not work. So to join you should use those field that are same type because it check equality . The $lookup stage does an

how can I create my mongodb query based on the input from the user in node.js?

别来无恙 提交于 2019-12-10 16:02:37
问题 Currently in my app I store different forum posts. Users can add new messages and create new posts. Other users - while displaying the content - can filter it so that they will not see the content uploaded by specific users that they blocked earlier. Each user is represented as a combination of device_id and display_name . When users are fetching content, they do a post query and include an array of previously blocked users: "blockedUsers": ( { "device_id" = "XXX"; "display_name" = XXX; }, {