mongodb-query

Mongo Embedded Document Query

隐身守侯 提交于 2019-12-21 14:33:04
问题 I've 2 DynamicDocuments: class Tasks(db.DynamicDocument): task_id = db.UUIDField(primary_key=True,default=uuid.uuid4) name = db.StringField() flag = db.IntField() class UserTasks(db.DynamicDocument): user_id = db.ReferenceField('User') tasks = db.ListField(db.ReferenceField('Tasks'),default=list) I want to filter the UserTasks document by checking whether the flag value (from Tasks Document) of the given task_id is 0 or 1 , given the task_id and user_id. So I query in the following way:- obj

Mongo Embedded Document Query

。_饼干妹妹 提交于 2019-12-21 14:32:43
问题 I've 2 DynamicDocuments: class Tasks(db.DynamicDocument): task_id = db.UUIDField(primary_key=True,default=uuid.uuid4) name = db.StringField() flag = db.IntField() class UserTasks(db.DynamicDocument): user_id = db.ReferenceField('User') tasks = db.ListField(db.ReferenceField('Tasks'),default=list) I want to filter the UserTasks document by checking whether the flag value (from Tasks Document) of the given task_id is 0 or 1 , given the task_id and user_id. So I query in the following way:- obj

MongoDB BasicDBObject vs Document in java

╄→гoц情女王★ 提交于 2019-12-21 12:22:29
问题 I am using MongoDB v3.2.0 with Mongo Java Driver 3.0.4 version. I am using the BasicDBObject (deprecated) instead of using the Document in java, as I need to do many changes for converting to Document in my standalone java project. Can anyone tell me changing into Document, will there be any performance improvement in memory and large collection inserts and reads. Is there any way to improve my frequent write and read operations on MongoDB using java. 回答1: Basic DBobject is not deprecated .

MongoDB: Update/Upsert vs Insert

怎甘沉沦 提交于 2019-12-21 06:59:38
问题 Recently I notice a huge performance difference between doing multiple upserts (via bulk operations) vs an insert (multiple documents). I would like to know if I am correctly on this: Upsert/Updates will be like a find() and update() so it does 2 things read and write Insert will just write so its a lot faster Thus the performance difference? If this is the case, I wonder if I need a lot of writes regularly, instead of updating a document, I write a new document with a createdOn field. Then

How to make comlex query MongoDB with Powershell

爷,独闯天下 提交于 2019-12-21 05:36:23
问题 I need to retrieve data from mongoDB using Powershell. Let's say I have db.orders collection and need to retrieve only orders created during last week and retrieve only specific columns for instance _id, status, createdAt fields. Orders collection schema { "_id": ObjectId("56cf9bab78e9fd46ec557d69"), "status" : "ordered", ... "total": 343, "createdAt": ISODate("2016-01-15T17:29:09.342Z") } I can query it in mongo shell like this db.orders.find({ "createdAt" : { $lt: new Date(), $gte: new Date

Distinct count of multiple fields using mongodb aggregation

孤人 提交于 2019-12-21 04:41:15
问题 I'm trying to count distinct values of multiple fields By one MongoDB Aggregation query. So here's my data: { "car_type": "suv", "color": "red", "num_doors": 4 }, { "car_type": "hatchback", "color": "blue", "num_doors": 4 }, { "car_type": "wagon", "color": "red", "num_doors": 4 } I want a distinct count of each field: distinct_count_car_type=3 distinct_count_color=2 distinct_count_num_doors=1 I was able to group multiple fields and then do a distinct count but it could only give me a count on

pymongo: remove duplicates (map reduce?)

醉酒当歌 提交于 2019-12-21 04:05:06
问题 I do have a Database with several collections (overall ~15mil documents) and documents look like this (simplified): {'Text': 'blabla', 'ID': 101} {'Text': 'Whuppppyyy', 'ID': 102} {'Text': 'Abrakadabraaa', 'ID': 103} {'Text': 'olalalaal', 'ID': 104} {'Text': 'test1234545', 'ID': 104} {'Text': 'whapwhapwhap', 'ID': 104} They all have an unique _id field as well, but I want to delete duplicates accodring to another field (the external ID field). First, I tried a very manual approach with lists

Aggregate/project sub-document as top-level document in mongo

元气小坏坏 提交于 2019-12-21 04:03:52
问题 After a few aggregation steps (pipeline steps) in one of my collections, I'm ending up with the following result: { "_id" : ObjectId("574e7722bffe901713d383bb"), "eventname" : "Ball Passed", "command" : { "_id" : ObjectId("57ec6b6f6c61e919b578fe7c"), "name" : "Run", "strike" : 15, "score" : true, "duration" : 123 } } { "_id" : ObjectId("57ec6b6f6c61e919b578ff8a"), "eventname" : "Ball Passed", "command" : { "_id" : ObjectId("573d688d080cc2cbe8aecbbc"), "name" : "Run", "strike" : 12, "score" :

How to prevent MongoDB from returning the object ID when finding a document?

安稳与你 提交于 2019-12-21 03:44:08
问题 I've the following document in MongoDB... { "_id" : ObjectId("531221cd960100960116b992"), "username : "joe", "address" : [ { "zip" : "8000", "city" : "Zurich" }, { "zip" : "6900", "city" : "Lugano" } ] } ... and to retrieve the second address I use the following statement: db.users.find({ _id: ObjectId("531221cd960100960116b992") }, { addresses: { $slice: [0, 1] } } ) This works except it also returns the object id: { "addresses" : [ { "zip" : "6900", "city" : "Lugano" } ], "_id" : ObjectId(

How to calculate difference between values of different documents using mongo aggregation?

戏子无情 提交于 2019-12-21 02:47:34
问题 Hi my mongo structure as below { "timemilliSec":1414590255, "data":[ { "x":23, "y":34, "name":"X" }, { "x":32, "y":50, "name":"Y" } ] }, { "timemilliSec":1414590245, "data":[ { "x":20, "y":13, "name":"X" }, { "x":20, "y":30, "name":"Y" } ] } Now I want to calculate difference of first document and second document and second to third in this way so calculation as below diffX = ((data.x-data.x)/(data.y-data.y)) in our case ((23-20)/(34-13)) diffY = ((data.x-data.x)/(data.y-data.y)) in our case