mongodb-query

Exception when geting distinct values of a field from Mongo collection using mongo Java Driver 3.0

冷暖自知 提交于 2019-12-11 03:47:40
问题 I'm getting distinct values of field from mongodb. When i run the following query in the commandline, it works good. db.celldata.distinct("tenentId") I'm using Mongo java 3.0 driver, to retrieve the distinct values using the following query MongoCursor<String> iterator = coll.distinct("tenantId", String.class).iterator(); When i run the query i get the following exception org.bson.BsonInvalidOperationException: readString can only be called when CurrentBSONType is STRING, not when

Grouping/counting sub-documents while preserving root fields

爱⌒轻易说出口 提交于 2019-12-11 03:45:26
问题 In mongodb, after a few $match and $project, I get the following 2 documents. I'm trying to figure out how to group/count together the list of states for each team in each group of each event. In short, I need to know how many teams are in each state (0, 1 or 2). I start out with the following documents. { "_id" : "event1", "groups" : [ { "_id" : "group1", "wlActive" : true, "teams" : [ {"state" : NumberInt(2)}, {"state" : NumberInt(2)}, {"state" : NumberInt(1)}, {"state" : NumberInt(1)}, {

How to dynamically build MongoDB Query with multiple OR?

和自甴很熟 提交于 2019-12-11 03:44:52
问题 I would like to be able to dynamically build a Mongo query using Node with multiple OR's. The point of this query is to do an AND search in the database with multiple search terms. For example, suppose someone wanted to search for records containing the words "dog" and "cereal." The query would only return records that have "dog" and "cereal" somewhere in the record (i.e. the search terms can appear in different columns). I know that the query should end up looking like this: query = { $and:

mongodb query nested structure

↘锁芯ラ 提交于 2019-12-11 03:43:23
问题 How can I query this nested json structure in order to find documents that contain "A"? "categories":[{"id":12,"values":[["A","B","C"]]},{"id":17,"values":[["D","E","F"]]}] So far, I only managed to get to the id value with db.coll.find( {categories: { $elemMatch: { id: 12 }}} ) 回答1: You need to nest the $elemMatch operators to match the nested levels of your arrays to match the element: db.coll.find({ "categories": { "$elemMatch": { "values": { "$elemMatch": { "$elemMatch": { "$in": ["A"] }

How to use $lookup and $in mongodb aggregate

寵の児 提交于 2019-12-11 03:19:37
问题 Colleges { "_id" : ObjectId("5cd42b5c65b41027845938ae"), "clgID" : "100", "name" : "Vivekananda" }, { "_id" : ObjectId("5cd42b5c65b41027845938ad"), "clgID" : "200", "name" : "National" } Point : 1 => Take all clgID From Colleges collection. Subjects: { "_id" : ObjectId("5cd42c2465b41027845938b0"), "name" : "Hindi", "members" : { "student" : [ "123" ] }, "college" : { "collegeID" : "100" } }, { "_id" : ObjectId("5cd42c2465b41027845938af"), "name" : "English", "members" : { "student" : [ "456",

Aggregate with count of sub documents matching the condition and grouping

£可爱£侵袭症+ 提交于 2019-12-11 03:15:01
问题 I've collections of documents as like as below: { "_id" : ObjectId("55d4410544c96d6f6578f893"), "executionProject" : "Project1", "suiteList" : [ { "suiteStatus" : "PASS" } ], "runEndTime" : ISODate("2015-08-19T08:40:47.049Z"), "runStartTime" : ISODate("2015-08-19T08:40:37.621Z"), "runStatus" : "PASS", "__v" : 1 } { "_id" : ObjectId("55d44eb4c0422e7b8bffe76b"), "executionProject" : "Project1", "suiteList" : [ { "suiteStatus" : "PASS" } ], "runEndTime" : ISODate("2015-08-19T09:39:13.528Z"),

mongodb - Finding the Sum of a field (if it exists) in a collection

一个人想着一个人 提交于 2019-12-11 03:14:12
问题 I have the following mongodb structure { "_id": ObjectId("507c80a143188f9610000003"), "date": ISODate("2012-10-15T21: 31: 13.0Z"), "uid": NumberInt(35920), "comp": NumberInt(770), "fields": { "rating": { "parent": "rating", "weight": NumberInt(2), "rel_weight": 0.11, }, "capacity": { "parent": "capacity", "weight": NumberInt(4), "rel_weight": 0.89, }, } } The "fields" attribute has 2 fields "rating" and "capacity" in it. But, each entry might have a different set of fields. eg. dimension,

“Join” two collection in MongoDB using node js

点点圈 提交于 2019-12-11 03:13:13
问题 I'm trying to make a query between two collections in mongodb using node js. I have these two collection in the db: Trip { "_id":ObjectId("55a922531e35772c1b17d4a0"), "name":"trip_name", "waypoints":[ "4c828999d8086dcb03877752", "4dd2ae657d8b4c6585f1a6fd", "4c59c3e4f346c928a8634dca" ], "line_points":[ [ 42.850937, 13.569256 ], [ 42.85109, 13.569377 ], [ 42.851131, 13.569225 ] ], "time":"00:10:23", "distance":6.622, "hashKey":"object:8207" }; Poi { "_id":"4c828999d8086dcb03877752", "type":

Mongo DB find all records with highest value depending on a key field

感情迁移 提交于 2019-12-11 03:12:52
问题 I would like to get for each user in my collection the comment of the record with the highest value. //myCol [ {'user' : 1, 'value':20, "comment": "cloudy", "date":"2018-12-01"}, {'user' : 2, 'value':5, "comment": "sun","date":"2018-12-01"}, {'user' : 3, 'value':13, "comment": "rain","date":"2018-13-01"}, {'user' : 4, 'value':13, "comment": "cloudy","date":"2018-13-01"}, {'user' : 2, 'value':20, "comment": "rain","date":"2018-01-20"}, {'user' : 1, 'value':2, "comment": "cloudy","date":"2018

Mongodb Java query for date range

泄露秘密 提交于 2019-12-11 03:05:00
问题 I needed to find all the records in mongo db within two date ranges using Mongo Driver[3.4.0] for Java. Example: I have books Collection. { "_id" : ObjectId("5acb40d27d63b61cb002bafe"), "title" : "WingsOfFire", "pub-date" : ISODate("2013-10-02T00:00:00.000Z"), "rel-date" : ISODate("2013-11-02T00:00:00.000Z") } Like above I have 100s of documents. I need to find all records wherein pub-date > rel-date. I am using Mongo DB version 3.2.6 I tried to use $expr operator but it seems to work with