mongodb-query

Get most recent Sub-Document from Array

邮差的信 提交于 2019-12-11 06:39:21
问题 I have an array. I would like to select the object with the highest revision number from my history arrays (plural). My document looks like this (often it will be more than just one object in uploaded_files ): { "_id" : ObjectId("5935a41f12f3fac949a5f925"), "project_id" : 13, "updated_at" : ISODate("2017-07-02T22:11:43.426Z"), "created_at" : ISODate("2017-06-05T18:34:07.150Z"), "owner" : ObjectId("591eea4439e1ce33b47e73c3"), "name" : "Demo project", "uploaded_files" : [ { "history" : [ {

How to get all matching items from a array of objects in MongoDB? [duplicate]

戏子无情 提交于 2019-12-11 06:22:59
问题 This question already has answers here : How to filter array in subdocument with MongoDB [duplicate] (3 answers) Closed 2 years ago . I have a mongo document like below { "_id" : ObjectId("588adde40fcbbbc341b34e1c"), "title" : "Fifa world cup", "tags" : [ { "name" : "Football", "type" : "Sports" }, { "name" : "World cup", "type" : "Sports" }, { "name" : "Fifa", "type" : "Manager" } ] } I wrote the below query to get all the tags with type Sports but I am only getting 1 item instead of 2 db

Java code for mongodb aggregation query with $in

让人想犯罪 __ 提交于 2019-12-11 06:18:27
问题 I am new to mongodb and need an equivalent code in java for the below db.asset.aggregate([{ $unwind : '$asset' }, { $match : { 'asset.status' : { $in : ['1', '2', '3'] }, 'asset.siteid' : { $in : ['123'] } } } ]).pretty() I tried the following but it didn't help-out DBObject unwind = new BasicDBObject("$unwind", "$dp.asset"); DBObject match = BasicDBObjectBuilder.start().push("$match") .push("dp.asset.status").add("$in", ['ACTIVE', 'LIMITEDUSE', 'OPERATING']) .push("dp.asset.siteid").add("$in

Mongodb 1to1 relation among subdocuments

╄→гoц情女王★ 提交于 2019-12-11 06:16:50
问题 I have a huge collection where each document has subdocuments that have relations among them. My schema looks like this: { userName: "user44", userID: "44", posts : [ ... { title : "post1", id : "123" ... }, { title : "post2", id : "124" ... }, ... ], comments: [ ... { id: 1910, postId : "123", title : "comment1", comment : "some comment", user: "user13" }, { id: 1911, postId : "124", title : "comment2", comment : "some comment", user: "user22" }, ... ], commentUpvotes: [ ... { id : 12,

Spring Data MongoDB aggregation - match by calculated value

感情迁移 提交于 2019-12-11 06:15:40
问题 I've got a collection of documents with 2 Number fields and I need to filter all the documents based on a calculated condition: Number1/Number2 >= CONST% How can I make this posible using Spring Data Mongo and Aggregation? --UPDATE-- I've tried the following code: return new RedactAggregationOperation( new BasicDBObject( "$redact", new BasicDBObject( "$cond", new BasicDBObject() .append("if", new BasicDBObject( "$gte", Arrays.asList(new BasicDBObject( "$divide", Arrays.asList( "$Number1", new

Retrieve only some fields from sub-document

余生颓废 提交于 2019-12-11 06:13:47
问题 (I am an extreme newbie with extremely simple question): If my database (from the documentation samples) consists of: { "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" } { "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" } { "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" } { "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" },

PYTHON - PYMONGO - Invalid Syntax with $or

隐身守侯 提交于 2019-12-11 06:07:20
问题 I can't seem to get this to work with pymongo it was working before I added the $or option. Am I missing something obvious with this dataout = releasescollection.find( { $or: [{"l_title":{"$regex": "i walk the line", "$options": "-i"}}, {"artistJoins.0.artist_name":{"$regex": "Johnny Cash", "$options": "-i"}}]}).sort('id', pymongo.ASCENDING).limit(25) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "discogs.py", line 51 dataout = releasescollection.find( { $or: [{

How to push or pull to embed array if exists in only a query mongodb

末鹿安然 提交于 2019-12-11 05:58:00
问题 I have a likes array embed in photos collection. Likes array contains user_id, Every time the user presses the like button, I need to check if the user_id exists in the like array. If it doesn't exist, I push user_id to likes array, else I pull it out of likes array. So I need to perform two queries. I want to encapsulate them in a single query. Photos: "likes" : [ { "user_id" : ObjectId("") }, { "user_id" : ObjectId("") } ], 来源: https://stackoverflow.com/questions/57840431/how-to-push-or

Cannot use $dayOfMonth aggregate with expression

无人久伴 提交于 2019-12-11 05:56:42
问题 So, I need to extract the day-of-week for some objects to make some aggregations. But all my documents have is a timestamp, not a Date. So I'm trying to use $dayOfMonth (and others) with an expression, I can't figure out what it is not working. Here is my query (along with a helper function to create my date from the timestamp): db.Logging.aggregate([ { $match: { "timestamp": { $gte: dateToTimestamp("2017-04-10") } } }, { $project: { _id: 0, timestamp: "$timestamp", dia: { $dayOfMonth: myDate

Find empty documents in a database

流过昼夜 提交于 2019-12-11 05:53:36
问题 I have queried an API which is quiet inconsistent and therefore does not return objects for all numerical indexes (but most of them). To further go on with .count() on the numerical index I've been inserting empty documents with db.collection.insert({}) My question now is: how would I find and count these objects? Something like db.collection.count({}) won't work obviously. Thanks for any idea! 回答1: Use the $where operator. The Javascript expression returns only documents containing a single