mongodb-query

Mongo Copy Collection and User Permissions

混江龙づ霸主 提交于 2020-01-02 14:50:11
问题 I am trying to copy a collection through the shell, but I am getting an 'unauthorized' error, despite having read/write permissions. Specifically: db.createCollection("ITISAFAKE") { "ok" : 1 } db.ITISAFAKE.insert({"Is it a fake?": true}) db.ITISAFAKE.find() { "_id" : ObjectId("52d5e51d4bb0851f985f69d8"), "Is it a fake?" : true } db.ITISAFAKE.drop() true Yay that works fine. However: db.createCollection("FAKE") { "ok" : 1 } db.FAKE.insert({senator: true}) db.FAKE.copyTo("FAKERY") Tue Jan 14 17

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

旧时模样 提交于 2020-01-02 13:33:10
问题 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"

Include fields in mongodb aggregate

早过忘川 提交于 2020-01-02 09:00:09
问题 I have the following collection: {"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "foo@bar.com"} {"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "foo@bar.com"} {"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "foo@bar.com"} {"orderID" : "89765", "branch" : "CO", "customerID" : "54157526", "customerEmail" : ""} {"orderID" : "89765", "branch" : "CO", "customerID" : "54157526",

How to search comma separated data in mongodb

陌路散爱 提交于 2020-01-02 07:09:44
问题 I have movie database with different fields. the Genre field contains a comma separated string like : {genre: 'Action, Adventure, Sci-Fi'} I know I can use regular expression to find the matches. I also tried: {'genre': {'$in': genre}} the problem is the running time. it take lot of time to return a query result. the database has about 300K documents and I have done normal indexing over 'genre' field. 回答1: Would say use Map-Reduce to create a separate collection that stores the genre as an

Can I set Mongoid query timeout? Mongoid don't kill long time query

时光总嘲笑我的痴心妄想 提交于 2020-01-02 07:05:47
问题 Mongoid don't have timeout option. http://mongoid.org/en/mongoid/docs/installation.html I want Mongoid to kill long time queries. How can I set Mongoid query timeout? If I do nothing, Mongoid wait a long time like the below. mongo > db.currentOp() { "opid" : 34973, "active" : true, "secs_running" : 1317, // <- too long! "op" : "query", "ns" : "db_name.collection_name", "query" : { "$msg" : "query not recording (too large)" }, "client" : "123.456.789.123:46529", "desc" : "conn42", "threadId" :

$push equivalent for map in mongo

天涯浪子 提交于 2020-01-02 06:58:15
问题 We can use $push (to add an element into an array) in update to atomically update a single document containing the array However, I could not find a way to atomically add a new key to a map in a document. I could * read the document, * read the map in it, * update the map in my code and * update the document in my code. But that is not atomic. I am only dealing with a single document but that document has a map. Is there a way I can update (add a new key) map atomically? 回答1: Dot notation

MongoDB text search filter by multiple fields

蹲街弑〆低调 提交于 2020-01-02 04:15:13
问题 I have the following document structure. { content: 'cat dog bird', uid: <another_unique_id> cid: <another_unique_id> } I am trying to search this collection and want to filter results by uid and/or cid . Some queries that I want to run: 1) db.mycollection.find({uid: '1', cid: '2', $text: {$search: 'cat'}}); 2) db.mycollection.find({cid: '2', $text: {$search: 'cat'}}); 3) db.mycollection.find({uid: '1', $text: {$search: 'cat'}}); 4) db.mycollection.find({$text: {$search: 'cat'}}); //etc... I

MongoDB $slice (embedded array paging)

谁说胖子不能爱 提交于 2020-01-02 03:51:08
问题 I have this schema: article: { subject, comments: [] } if I have 8 comments, and query article.find({}, { comments: { $slice: [ -10, 5 ] } }); And I get comments from index 0 to index 4, but I only want comments from index 0 to index 2 to be returned because of paging. (page 1 $slice[ -5, 5 ] from index 3 to index 7, page 2 $slice[ -10, 5 ] from index 0 to index 2) now I have to pass another parameter "lastId" to compare each comments and remove that "_id" < "lastId", but I think it is a

How to Convert mongodb ISODate to string in mongoDB?

我只是一个虾纸丫 提交于 2020-01-02 03:26:45
问题 I have my ISODate in mongo as ISODate and I want to just that in string format with a specific datetime format. Here is the ISODate: ISODate("2020-04-24T11:41:47.280Z") Expected Result: "2020-04-24T11:41:47.280Z" I want this to be happened on mongodb only as many of my services are expecting in this format, and I don't want to make changes in all services as its a tedious job. 回答1: I got the expected result while i was trying the following. ISODate("2020-04-24T11:41:47.280Z").toJSON() This

Full text search in mongodb in .net

℡╲_俬逩灬. 提交于 2020-01-02 03:15:09
问题 I have to search contents in all documents in particular collection of mongodb in .net mvc . I have tried with mongodb shell by creating index successfully like here . db.collection_name.createIndex( { subject: "text" } ) db.collection_name.find( { $text: { $search: "search_word" } } ) It works fine . but when i put it in .net that gives me error . I googled it and got following solution for indexing . collection.EnsureIndex(new IndexKeysBuilder().Ascending("subject")); now how can i run this