mongodb-query

Loop through Mongo Collection and update a field in every document

二次信任 提交于 2019-12-05 14:23:37
I have Dates in one Collection that were inserted incorrectly, and are in a simple "2015-09-10" string format. I'd like to update them to correct ISO Date format . I've tried looping through Mongo with forEach() but I don't know the shell well enough on how to update each document in the collection. So far I'm at this point: db.getCollection('schedules').find({}).forEach(function (doc) { doc.time = new Date( doc.time ).toUTCString(); printjson( doc.time ); // ^ This just prints "Invalid Date" // Also none of the below work when I try saving them //doc.save(); //db.getCollection('schedules')

Mongodb - bad query: BadValue unknown top level operator: $gte

亡梦爱人 提交于 2019-12-05 13:40:19
问题 What is wrong with this query? I tried to run it on mongodb server and received an error as following - "exception: bad query: BadValue unknown top level operator: $gte". Can anyone tell me what is wrong with it, please? db.scores.aggregate([ { $match: { $or: [ { $gte: [ "$score", 30 ] }, { $lte: [ "$score", 60 ] } ] } }, { $group: { _id: "$gamer", games: { $sum: 1 } } } ]) sample data : { "_id" : "545665cef9c60c133d2bce72", "score" : 85, "gamer" : "Latern" } /* 1 */ { "_id" :

mongoDB: how to reverse $unwind

a 夏天 提交于 2019-12-05 11:52:18
Consider this collection of test results: [{ _id: ObjectId(...), name: "Test1", acts: [ { name: "act1", tests: [ {name: "test1", result: true}, {name: "test2", result: true}] }] }, { _id: ObjectId(...), name: "Test2", acts: [ { name: "act1", tests: [ {name: "test1", result: true}, {name: "test2", result: false}] }, { name: "act2", tests: [ {name: "test3", result: true}] }] }] I'm trying to use aggregations to create a calculated field with the sum of all test results, I want something like this: [{ _id: ObjectId(...), name: "Test1", result: true, //new aggregated value acts: [ { name: "act1",

Mongo - How to update multiple documents with different value in single query?

谁说胖子不能爱 提交于 2019-12-05 10:56:49
I want to write a query for updating multiple documents in a single query, Please suggest me possible ways. Following is my mongo document. [ { "_id" : ObjectId("5b0f0a2ca1f6633032c204cd"), "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"), "name" : "ABC", "userType": "admin" }, { "_id" : ObjectId("5b0f0a2ca1f6633032c204ce"), "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"), "name" : "DEF", "userType": "admin" }, { "_id" : ObjectId("5b0f0a2ca1f6633032c204cf"), "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"), "name" : "GHI", "userType": "admin" }, { "_id" : ObjectId(

Find all collections in mongodb with specific field

蹲街弑〆低调 提交于 2019-12-05 10:36:16
There is more than 40 collections in database I am currently working on. One of the major key in all the collections is "account". I need to know all such collections where there is a field called "account". Is there a query to get or a js script which prints all such collections? In Oracle I was using : SELECT * FROM ALL_TAB_COLUMNS WHERE COLUMN_NAME LIKE 'account'; Any inputs is helpful. Thanks in advance. The following mongo script will print out all collection names where at least one document contains an account field. db.getCollectionNames().forEach(function(collname) { var count = db

Find users who has not invited any user

筅森魡賤 提交于 2019-12-05 10:13:45
I want to find those users who has not invited any user by a single query (using aggregate). for Example : I have 4 users in DB { "_id" : ObjectId("581a18d41b6c5c752f11c87a"), "name": "aaa", "invitedBy": ObjectId("5808f53d28c14ee470856d8b") }, { "_id" : ObjectId("581a1a671b6c5c752f11c87b"), "name": "bbb", "invitedBy": ObjectId("581a18d41b6c5c752f11c87a") }, { "_id" : ObjectId("581a1a671b6c5c752f11c87c"), "name": "ccc", "invitedBy": ObjectId("581a18d41b6c5c752f11c87a"), }, { "_id" : ObjectId("581a1a671b6c5c752f11c87d"), "name": "ddd", "invitedBy": ObjectId("581a1a671b6c5c752f11c87b"), } Here 1-

A pipeline stage specification object must contain exactly one field with php mongo aggregate

断了今生、忘了曾经 提交于 2019-12-05 09:53:20
I am trying to use aggregate with with project, match and sort but i am getting an exception ( MongoResultException to be exact) saying exception: A pipeline stage specification object must contain exactly one field. It works fine when I did not use sort and limit but I need them for this. The reason I am not using find() because I read somewhere that it can increase performance. Please help $query = array(.... //An actual query that works with find() $collection = $this->db->CollectionName; $project = array( '$project' => array( 'Field1' => 1, 'Field2'=> 1, 'Field3'=> 1, 'Field4' => 1 ) );

Using golang and mgo, how do I search for a range of values in MongoDB?

丶灬走出姿态 提交于 2019-12-05 09:30:34
I worked through the example on the mgo homepage , but I'm struggling to find a way to query a range of values. The line: searchResults, searchErr = SearchReading(bson.M{"k": key, "t": { $gte: start, $lte: end } }, limit) fails with: line67: syntax error: unexpected $ line67: missing type in composite literal I left out the non-essential bits of code... type Reading struct { K string "k" T int64 "t" V float64 "v" } func SearchReading(q interface{}, limit int) (searchResults []Reading, searchErr string) { searchErr = "" searchResults = []Reading{} query := func(c *mgo.Collection) error { fn :=

How to store unsigned long long (uint64_t) values in a MongoDB document?

痴心易碎 提交于 2019-12-05 09:25:19
I want to store numbers of type unsigned long long (uint64_t) in a MongoDB document, how do I do it? I need to use unsigned long long because I'm using Twitter API which uses unsigned 64 bit integers https://dev.twitter.com/docs/twitter-ids-json-and-snowflake The range of the the unsigned 64 bit integral type needs to be represended by 8 bytes and with a data range of 0 to 18,446,744,073,709,551,615. I'm using the C++ MongoDB driver and the append member function of the BSONArrayBuilder class doesn't have an overload for unsigned long long, only for long long. Here's the error G++ 4.7.2 spits

MongoDB text search filter by multiple fields

烈酒焚心 提交于 2019-12-05 09:22:29
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 tried to create a compound index like this db.mycollection.ensureIndex({uid: 1, cid: 1, content: 'text'