mongodb-query

Mongo db aggregation multiple conditions

我的梦境 提交于 2020-01-02 00:57:14
问题 I want to project a collection applying exporting a value only if a field is inside a range. Sort of: db.workouts.aggregate({ $match: { user_id: ObjectId(".....") } }, { $project: { '20': { $cond: [ {$gt: [ "$avg_intensity", 20]} , '$total_volume', 0] } } }) I need to get the value only if the avg_intensity is inside a certain range. I will then group and sum on the projection result. What I am trying to do is applying a $gt and $lt filter but with no much success. db.workouts.aggregate( {

Want to Update Array of Array Object Value in Mongo

我的未来我决定 提交于 2020-01-01 18:04:34
问题 I have the following MongoDB document: { "_id": ObjectId(), "sku": "V4696-DR-V33", "options": [ { "sku": "8903689984338", "stores": [ { "code": "AND1", "zipcode": "110070", "inventory": -1000 }, { "code": "AND2", "zipcode": "201010", "inventory": -1000 }, { "code": "AND3", "zipcode": "411001", "inventory": -1000 }, { "code": "AND4", "zipcode": " 700020", "inventory": -1000 }, { "code": "AND5", "zipcode": "110015", "inventory": -1000 } ], "price": 2199, "_id": ObjectId(), "size": "14" }, {

MongoDB sort by only exists entry, key with value first and key with null or not exists last

笑着哭i 提交于 2020-01-01 16:39:04
问题 I have a collection of 15000 documents. Some documents have sr_no with numeric values and other with absent of sr_no . Now i want to get entries like all documents comes first which has sr_no with asc then all others. I tried .find().sort({sr_no:1}) but it return all null entries first then asc with sr_no . This question seems too close with duplicate. But slightly defer with numeric key. I answered it with hack below. 回答1: I used a dirty hack for this. MongoDB doc says that they have

How to validate in Mongoose an array and the same time its elements

醉酒当歌 提交于 2020-01-01 11:49:35
问题 I have this schema where I validated the elements of array book , but I don't know how to validate the array itself. var DictionarySchema = new Schema({ book: [ { 1: { type: String, required: true }, 2: String, 3: String, c: String, p: String, r: String } ] }); For example, I would like to put the book array as required. Any help? 回答1: You can use a custom validator to do this. Simply check that the array itself is not empty: var mongoose = require('mongoose'), Schema = mongoose.Schema;

Collection ID length in MongoDB

梦想的初衷 提交于 2020-01-01 04:25:10
问题 i am new to mongodb and stack overflow. I want to know why on mongodb collection ID is of 24 hex characters? what is importance of that? 回答1: Why is the default _id a 24 character hex string? The default unique identifier generated as the primary key ( _id ) for a MongoDB document is an ObjectId. This is a 12 byte binary value which is often represented as a 24 character hex string, and one of the standard field types supported by the MongoDB BSON specification. The 12 bytes of an ObjectId

Why MongoDB cannot use a compound index that is much similar(not exact) to the query?

大城市里の小女人 提交于 2020-01-01 03:53:10
问题 Consider the below Mongo index strategy and the query, Index: db.collec.ensureIndex({a:1,b:1,c:1}); Query: db.collec.find({"a":"valueA"},{"_id":0,"a":1,"c":1}).sort({"c":-1}).limit(150) The explain on the above query returns: /* 0 */ { "cursor" : "BtreeCursor a_1_b_1_c_1", "isMultiKey" : false, "n" : 150, "nscannedObjects" : 178, "nscanned" : 178, "nscannedObjectsAllPlans" : 279, "nscannedAllPlans" : 279, "scanAndOrder" : true, "indexOnly" : true, "nYields" : 0, "nChunkSkips" : 0, "millis" :

Spring-Data mongodb querying multiple classes stored in the same collection

。_饼干妹妹 提交于 2020-01-01 03:23:10
问题 With Spring-Data, you can use the @Document annotation to specify which collection to save the object to. Say I have two classes, Student and Teacher, both persisted into the people collection. When I execute the following code: mongo.find(new Query(), Teacher.class); the result contains both Student and Teacher. Looking in the data created by Spring-Data, each document contains a "_class" field which indicate which class it is persisted from. This is field not used in find as an filter to

How to perform a bulk update of documents in MongoDB with Java

倾然丶 夕夏残阳落幕 提交于 2019-12-31 09:35:09
问题 I'm using MongoDB 3.2 and MongoDB Java Driver 3.2. I have an array of a couple of hundreds of updated documents which should be now saved/stored in MongoDB. In order to do that, I iterate over the array and call for each document in this array the updateOne() method. Now, I want to re-implement this logic with a bulk update. I tried to find an example of bulk update in MongoDB 3.2 with MongoDB Java Driver 3.2. I tried this code: MongoClient mongo = new MongoClient("localhost", 27017); DB db =

How to count items in list of lists

て烟熏妆下的殇ゞ 提交于 2019-12-31 05:22:49
问题 How i can find length of each item in a "mol" list: For example if I am looking for "versionA" : "2.1.2" I expect ot get following result: {"project" : "A", "scene" : 13455312, mol:[3,2,1]}, {"project" : "B", "scene" : 13455344, mol:[3,3,3]} My data: { "project" : "A", "scene" : 13455312, "versionA" : "2.1.2", "versionB" : "2.1.9", "mol" : [ {"data" : [ { "tran" : 'A' }, { "tran" : 'B' }, { "tran" : 'C' }]}, {"data" : [ { "tran" : 'A' }, { "tran" : 'B' }]}, {"data" : [ { "tran" : 'A' }]}] },

Count events and insert string literal during aggregation

我的未来我决定 提交于 2019-12-31 04:04:29
问题 I have large collection of documents which represent some kind of events. Collection contains events for different userId. { "_id" : ObjectId("57fd7d00e4b011cafdb90d22"), "userId" : "123123123", "userType" : "mobile", "event_type" : "clicked_ok", "country" : "US", "timestamp" : ISODate("2016-10-12T00:00:00.308Z") } { "_id" : ObjectId("57fd7d00e4b011cafdb90d22"), "userId" : "123123123", "userType" : "mobile", "event_type" : "clicked_cancel", "country" : "US", "timestamp" : ISODate("2016-10