mongodb-query

mongodb wildcard match all values for specific key [duplicate]

风流意气都作罢 提交于 2019-12-06 10:39:50
This question already has answers here : Check that Field Exists with MongoDB (4 answers) Closed 8 months ago . I am trying to figure out how to match a key and return all the values for that key. Is it possible to give the value as a wildcard? I want to return everything for that specific key using wildcard on the value. db.collection.find({"key" :"*"}) Also I was hoping this would return the entire collection as well that had the key with the wildcard value match as well. You may be looking for something like this: db.collection.find({"key": {$exists: true}}) This will return all documents

$sum from documents and subdocuments group by “$author” (MongoDB)

て烟熏妆下的殇ゞ 提交于 2019-12-06 10:30:21
This is my collection: { "_id" : 10926400, "votes": 131, "author": "Jesse", "comments" : [ { "id" : 1, "votes": 31, "author": "Mirek" }, { "id": 2, "votes": 13, "author": "Leszke" } ] }, { "_id" : 10926401, "votes": 75, "author": "Mirek", "comments" : [ { "id" : 1, "votes": 17, "author": "Jesse" }, { "id": 2, "votes": 29, "author": "Mirek" } ] } And I want $sum values of votes and comments.votes of each author expected output( sort $votes: -1 ): "Mirek" total votes: 31 + 75 + 29 = 135 "Jesse" total votes: 131 + 17 = 148 "Leszke total votes: 13 Not immediately visible but possible. What you

mongoDB : updating an objects using dot notation (multi-level object)

浪子不回头ぞ 提交于 2019-12-06 09:39:30
I have an object with the following form : { "_id": ObjectId("4fa43f4d1cf26a6a8952adf1"), "userId": "1", "facebookId": "1234", "groups": [{ "groupName": "testGroup", "members": [{ "memberFirstName": "userFirstName", "memberLastName": "userLastName", "memberDetails": { "userId": "1", "faceBookId": "1234" } }] }] } It's a collection that holds for each user - its groups, with each group containing the group members... So the "groups" key is an array (or list), and so is the "members" key. (each user could have multiple groups, each group has multiple group members). I'm having a hard time

Why MongoDB different query plans show different nReturned value?

我的未来我决定 提交于 2019-12-06 09:39:21
I have a collection faults in my MongoDB database which every document has these fields: rack_name , timestamp Just for sake of testing and comparing performances, I have created these two indexes: rack -> {'rack_name': 1} and time -> {'timestamp': 1} Now I executed the following query with explain(): db.faults.find({ 'rack_name': { $in: [ 'providence1', 'helena2' ] }, 'timestamp': { $gt: 1501548359000 } }) .explain('allPlansExecution') and here is the result: { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "quicktester_clone.faults", "indexFilterSet" : false, "parsedQuery" : { "$and"

Filter $lookup results

佐手、 提交于 2019-12-06 09:18:24
I have 2 collections (with example documents): reports { id: "R1", type: "xyz", } reportfiles { id: "F1", reportid: "R1", time: ISODate("2016-06-13T14:20:25.812Z") }, { id: "F14", reportid: "R1", time: ISODate("2016-06-15T09:20:29.809Z") } As you can see one report may have multiple reportfiles . I'd like to perform a query, matching a report id , returning the report document as is, plus an additional key storing as subdocument the reportfile with the most recent time (even better without reportid , as it would be redundant), e.g. { id: "R1", type: "xyz", reportfile: { id: "F14", reportid:

How to dynamically build mongodb query

安稳与你 提交于 2019-12-06 08:08:02
I have a match expression in a mongodb aggregation. There are 3 fields that are included in the match but they don't all always contain data. I only want to include the fields in the match if the field isn't empty. This is what the match looks like if all fields have data but for example, if the array used for studentGradeLevels is empty, then I don't want to include it or I want the query to still return data ignoring the empty parameter. $match: { "school._id": "7011", "studentGradeLevels": { $in: ["09", "10", "11", "12"] }, "contentArea": { $in: [ "English 1" ] } } Is there a way to either

find first of documents for each distinct values for one field

妖精的绣舞 提交于 2019-12-06 07:17:20
With a collection of documents with fields field1 , field2 , field3 and so on, I need to find distinct values for field3 for each distinct value of field3 , need to get the first document with each distinct value in field3 For # 1, I could do db.myCollection.distinct("field3") How do I go about #2 ? Sample Collection: [ { "field1": 11, "field2": "toyota", "field3": "camry" }, { "field1": 22, "field2": "toyota", "field3": "corolla" }, { "field1": 33, "field2": "toyota", "field3": "camry" }, { "field1": 44, "field2": "honda", "field3": "accord" }, { "field1": 55, "field2": "honda", "field3":

$lookup when foreign field is an array

 ̄綄美尐妖づ 提交于 2019-12-06 06:36:50
问题 I have two collections. Sports : { "_id" : ObjectId("5bcaf82120e047301b443c06"), "item_name" : "Football", "item_icon" : "ps_icon_football.png", "slot_divisions" : { "0" : { "div_id" : ObjectId("5bd037ec5021b307e793f7b3"), "description" : "5x5" }, "1" : { "div_id" : ObjectId("5bd0384b5021b307e793f7b4"), "description" : "7x7" } }) Booking : { "_id" : ObjectId("5be015bd870565038c7660f4"), "spot" : ObjectId("5bd825cb8705651b1c2f17e2"), "date" : ISODate("2018-11-13T10:04:45.000Z"), "slots" : [ {

How to use $regex inside $or as an Aggregation Expression

家住魔仙堡 提交于 2019-12-06 06:35:48
问题 I have a query which allows the user to filter by some string field using a format that looks like: "Where description of the latest inspection is any of: foo or bar " . This works great with the following query: db.getCollection('permits').find({ '$expr': { '$let': { vars: { latestInspection: { '$arrayElemAt': ['$inspections', { '$indexOfArray': ['$inspections.inspectionDate', { '$max': '$inspections.inspectionDate' }] }] } }, in: { '$in': ['$$latestInspection.description', ['Fire inspection

MongoDB dateDiff between multiple documents

♀尐吖头ヾ 提交于 2019-12-06 06:16:48
I have collection in my mongoDB which stores service given to customer along with their email address something like below { "_id" : ObjectId("56a84627f8fd4a136c0e944a"), "Vehicle" : "Honda", "ServiceSelected" : "FULL SERVICE", "FullName" : "xyz", "Email" : "xyz@xyz.com", "BookingTime" : ISODate("2015-12-27T06:00:00.000Z") }, { "_id" : ObjectId("56a84627f8fd4a136c0e944b"), "Vehicle" : "AUDI", "ServiceSelected" : "FLAT TYRE", "FullName" : "abc", "Email" : "abc@abc.com", "BookingTime" : ISODate("2015-12-26T06:00:00.000Z") }, { "_id" : ObjectId("56a84627f8fd4a136c0e944c"), "Vehicle" : "BMW",