mongodb-query

add fields where condition match to nested array

筅森魡賤 提交于 2019-12-04 20:35:33
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" : ObjectId("5afd257daf18d985a06ac6ac"), "recipient" : ObjectId("5afadfdf08a7aa6f1a27d986"), "requester" :

structure of documents for versioning of a time series on mongodb

有些话、适合烂在心里 提交于 2019-12-04 19:38:52
I have a time series that grows and is (potentially) revised through time: on "2013-01-01": First version of the data "2013-01-01" 10 on "2013-01-02": Data of the 1st of Jan is revised from 10 to 11 "2013-01-01" 11 on "2013-02-01": First version of the data of the 1st of Feb "2013-01-01" 11 "2013-02-01" 20 on "2013-02-02": Data of the 1st of Feb is revised from 20 to 21 "2013-01-01" 11 "2013-02-01" 21 most frequent queries: query1: get the most recent version of all dates "2013-01-01" 11 "2013-02-01" 21 query2: get the time series as it was known at a certain date: For instance, querying with

How to remove document from all collections in Mongo database

坚强是说给别人听的谎言 提交于 2019-12-04 19:27:14
I have Mongo database with 16 collections. All collection has the common field domain_id . How can I remove documents with specified domain_id from all collections. I know only how to remove document from single collection. db.getCollection('collectionName1').remove({domain_id : '123'}) Use the method db.getCollectionNames() to get a list of all the collections in your database, iterate the list using the JavaScript's forEach() method to remove the document from each collection: db.getCollectionNames().forEach(function (col) { db.getCollection(col).remove({domain_id : '123'}) }); Unfortunately

Mongodb Aggregate nested subdocuments

萝らか妹 提交于 2019-12-04 19:16:07
Having list of documents [ { "__v" : 21, "_id" : ObjectId("546330dbb8926d177052e9ff"), "code" : "WfvCc", "description" : "", "elements" : [ { "_id" : ObjectId("546471f61e13b76a0b20ccaf"), "comments" : [], "meta" : { "createdBy" : "545ab39ef1b0c88a695fcf8d", "modifiedAt" : "1415868918045", "createdAt" : "1415868918045" }, "title" : "awesome title", "votes" : { "count" : 3, "meta" : [ { "createdBy" : "545ab39ef1b0c88a695fcf8d", "_id" : ObjectId("546473831e13b76a0b20ccb7"), "createdAt" : "1415869315618" }, { "createdBy" : "545aaddcf1b0c88a695fcf84", "_id" : ObjectId("546473d71e13b76a0b20ccbc"),

Flatten a nested object in MongoDB and rename

馋奶兔 提交于 2019-12-04 19:07:31
Suppose I have documents like this { "id" : "1415166669", "color" : { "14" : "Silver" }, "name":"Random Name" } where the key inside color can change, i.e Next Document may look like this { "id" : "1415126969", "color" : { "11" : "Gold" }, "name":"Random Name 2" } I want to flatten and rename them so that my documents have a similar structure as follows: { "id" : "1415126969", "color" : "Gold" "name":"Random Name 2" } and { "id" : "1415166669", "color" : "Silver" "name":"Random Name" } I tried using the aggregation pipeline but I am not sure how to proceed further. Also can anyone suggest a

PyMongo create unique index with 2 or more fields

泄露秘密 提交于 2019-12-04 18:32:18
问题 How can i create index in pymongo with 2 fields, to be unique together? I have this code: self.db[self.mongo_collection].create_index("url", unique=True) but i need to be unique with url and category . 回答1: You need to create a compound index and set unique to True as mentioned in the documentation: If you use the unique constraint on a compound index, then MongoDB will enforce uniqueness on the combination of values rather than the individual value for any or all values of the key. self.db

Mongodb aggregation lookup with conditions

回眸只為那壹抹淺笑 提交于 2019-12-04 17:59:15
问题 I have a collection called article_category which store all article_id belongs to the category with category_id with data format like so. Collection 1: article_category { "article_id": 2015110920343902, "all_category_id": [5,8,10] } Then I have other collection called article which store all my post Collection 2: article { "title": "This is example rows in article collection" "article_id": 2015110920343902, }, { "title": "Something change" "article_id": 2015110920343903, }, { "title": "This

storing a scanned (pdf,tiff,jpeg) file in MongoDB .

耗尽温柔 提交于 2019-12-04 17:45:25
I have to store a tiff(tag image file format) or pdf scanned file in mongodb that should be Text search able . like if we want to search "on base of text" it should be able to search . I am going to use .net mvc or java with mongodb . so how can i store this pdf file and then can retrieve from database . any suggestion will be appreciated . thanks shA.t You can store files by using MongoDb GridFs as described in this question and extract texts from a PDF file by using some features those described in this question . ;). HTH I think that you should save the files on file system of the server

Mongodb $in against a field of objects of array instead of objects of array

我的未来我决定 提交于 2019-12-04 17:42:31
问题 arr=[{field1:<value1>,field2:<value2}.....] I want to use the $in operator against the field1 of arr . I know I could create a dummy array and push the field1 values in the dummy array. But is there a way to use the $in operator against a particular field of an array? The arr array is no way related to the collection. I want to query all the documents on a particular field whose value is in the field1 of arr - field1 should be in the right hand side of operator $in Example: arr=[{name:'foo'

MongoDB lists - get every Nth item

北城余情 提交于 2019-12-04 17:40:51
问题 I have a Mongodb schema that looks roughly like: [ { "name" : "name1", "instances" : [ { "value" : 1, "date" : ISODate("2015-03-04T00:00:00.000Z") }, { "value" : 2, "date" : ISODate("2015-04-01T00:00:00.000Z") }, { "value" : 2.5, "date" : ISODate("2015-03-05T00:00:00.000Z") }, ... ] }, { "name" : "name2", "instances" : [ ... ] } ] where the number of instances for each element can be quite big. I sometimes want to get only a sample of the data, that is, get every 3rd instance, or every 10th