mongodb-query

How to get back the new value after an update in a embedded array?

泪湿孤枕 提交于 2019-12-17 10:05:33
问题 Given the following MongoDB collection: { "_id": ObjectId("56d6a7292c06e85687f44541"), "name": "My ranking list", "rankings": [ { "_id": ObjectId("46d6a7292c06e85687f55542"), "name": "Ranking 1", "score": 1 }, { "_id": ObjectId("46d6a7292c06e85687f55543"), "name": "Ranking 2", "score": 10 }, { "_id": ObjectId("46d6a7292c06e85687f55544"), "name": "Ranking 3", "score": 15 }, ] } Here is how I increase the score of a given ranking: db.collection.update( { "_id": ObjectId(

Combining $regex and $or operators in Mongo

若如初见. 提交于 2019-12-17 10:05:33
问题 I want to use $or and $regex operators same time. db.users.insert([{name: "Alice"}, {name: "Bob"}, {name: "Carol"}, {name: "Dan"}, {name: "Dave"}]) Using $regex works fine: > db.users.find({name: {$regex: "^Da"}}) { "_id" : ObjectId("53e33682b09f1ca437078b1d"), "name" : "Dan" } { "_id" : ObjectId("53e33682b09f1ca437078b1e"), "name" : "Dave" } When introducing $or , the response is changed. I expected the same response: > db.users.find({name: {$regex: {$or: ["^Da"]}}}) { "_id" : ObjectId(

How to get back the new value after an update in a embedded array?

馋奶兔 提交于 2019-12-17 10:05:15
问题 Given the following MongoDB collection: { "_id": ObjectId("56d6a7292c06e85687f44541"), "name": "My ranking list", "rankings": [ { "_id": ObjectId("46d6a7292c06e85687f55542"), "name": "Ranking 1", "score": 1 }, { "_id": ObjectId("46d6a7292c06e85687f55543"), "name": "Ranking 2", "score": 10 }, { "_id": ObjectId("46d6a7292c06e85687f55544"), "name": "Ranking 3", "score": 15 }, ] } Here is how I increase the score of a given ranking: db.collection.update( { "_id": ObjectId(

How to pull one instance of an item in an array in MongoDB?

为君一笑 提交于 2019-12-17 10:00:12
问题 According to the documents: The $pull operator removes from an existing array all instances of a value or values that match a specified condition. Is there an option to remove only the first instance of a value? For example: var array = ["bird","tiger","bird","horse"] How can the first "bird" be removed directly in an update call? 回答1: So you are correct in that the $pull operator does exactly what the documentation says in that it's arguments are in fact a "query" used to match the elements

Multiple Counts with single query in mongodb

﹥>﹥吖頭↗ 提交于 2019-12-17 06:54:35
问题 I am new to Mongo Db and would appreciate some help with this query. I have been sifting through posts here for the past couple of days tearing my hair to see if I could find anything related to my query but with no luck. I have a collection with documents similar in structure to below : _id: xyz Movieid: 123 MovieName: Titanic ReleaseDate: 2000-01-01 _id: uvw Movieid: 456 MovieName: Titanic II ReleaseDate: 2018-01-01 _id: pqr Movieid: 789 MovieName: Titanic III ReleaseDate: I would like to

Multiple Counts with single query in mongodb

江枫思渺然 提交于 2019-12-17 06:54:07
问题 I am new to Mongo Db and would appreciate some help with this query. I have been sifting through posts here for the past couple of days tearing my hair to see if I could find anything related to my query but with no luck. I have a collection with documents similar in structure to below : _id: xyz Movieid: 123 MovieName: Titanic ReleaseDate: 2000-01-01 _id: uvw Movieid: 456 MovieName: Titanic II ReleaseDate: 2018-01-01 _id: pqr Movieid: 789 MovieName: Titanic III ReleaseDate: I would like to

Remove Duplicates from MongoDB

﹥>﹥吖頭↗ 提交于 2019-12-17 06:14:40
问题 hi I have a ~5 million documents in mongodb (replication) each document 43 fields. how to remove duplicate document. I tryed db.testkdd.ensureIndex({ duration : 1 , protocol_type : 1 , service : 1 , flag : 1 , src_bytes : 1 , dst_bytes : 1 , land : 1 , wrong_fragment : 1 , urgent : 1 , hot : 1 , num_failed_logins : 1 , logged_in : 1 , num_compromised : 1 , root_shell : 1 , su_attempted : 1 , num_root : 1 , num_file_creations : 1 , num_shells : 1 , num_access_files : 1 , num_outbound_cmds : 1

exclude fields in $lookup aggregation

假如想象 提交于 2019-12-17 05:15:13
问题 I am querying between 3 collections I want to exclude _id everywhere in output My output is: { "_id" : ObjectId("5b6aed5f9bcdb5d4ae64aef5"), "userID" : "1", "skills" : [ { "_id" : ObjectId("5b766b5f1365a4940bb6050f"), "skillID" : "javaid", "skillname" : "जावा", "languageID" : "hindiid" }, { "_id" : ObjectId("5b766b8c1365a4940bb60535"), "skillID" : "pythonid", "skillname" : "पायथन", "languageID" : "hindiid" } ], "gender" : { "_id" : ObjectId("5b7687cd2a2329043e2383d5"), "genderID" : "femaleid"

mongodb: insert if not exists

此生再无相见时 提交于 2019-12-17 03:52:21
问题 Every day, I receive a stock of documents (an update). What I want to do is insert each item that does not already exist. I also want to keep track of the first time I inserted them, and the last time I saw them in an update. I don't want to have duplicate documents. I don't want to remove a document which has previously been saved, but is not in my update. 95% (estimated) of the records are unmodified from day to day. I am using the Python driver (pymongo). What I currently do is (pseudo

Matching ObjectId to String for $graphLookup

旧时模样 提交于 2019-12-17 02:51:34
问题 I'm trying to run a $graphLookup like demonstrated in print bellow: The objective is to, given a specific record (commented $match there), retrieve it's full "path" throught immediateAncestors property. As you can see, it's not happening. I introduced $convert here to deal with _id from collection as string , believing it could be possible to "match" with _id from immediateAncestors records list (which is a string ). So, I did run another test with different data (no ObjectId s involved): db