mongo-shell

Update query in MongoDB shell

久未见 提交于 2019-11-30 11:57:26
问题 In the shell, my query is: db.checkin_4e95ae0926abe9ad28000001.update({location_city:"New York"}, {location_country: "FUDGE!"}); However, it doesn't actually update my records. It doesn't error either. When I do a db.checkin_4e95ae0926abe9ad28000001.find({location_city:"New York"}); after running this, I get all my results but the location_country has not changed: { "_id": ObjectId("4e970209a0290b70660009e9"), "addedOn": ISODate("2011-10-13T15:21:45.772Z"), "location_address1": "", "location

Update in forEach on mongodb shell

南笙酒味 提交于 2019-11-30 04:48:17
I have got a collection aTable with 2 records: { "title" : "record 1", "fields" : [ { "_id" : 1, "items" : [ 1 ] }, { "_id" : 2, "items" : [ 2,3,4 ] }, { "_id" : 3, "items" : [ 5 ] } ] }, { "title" : "record 2", "fields" : [ { "_id" : 4, "items" : [ 7,8,9,10 ] }, { "_id" : 5, "items" : [ ] }, { "_id" : 6, "items" : [ 11,12 ] } ] } I want to update fields aTable.fields.items from items : [ 11,12 ] to items : [ {item: 11, key: 0}, {item:12, key: 0} ] I browse fields with forEach but I can't save it: var t = db.aTable.find(); t.forEach(function( aRow ) { aRow.fields.forEach( function( aField ){

How do you connect to a replicaset from a MongoDB shell?

浪子不回头ぞ 提交于 2019-11-29 20:07:32
If I'm writing an application which connects to mongodb then I can provide a seed list for a replicaset, and the driver will direct me to the master node, where I can run write commands. How do I specify the seed list for a commandline mongo shell in order to conenct to a replicaset. Gianfranco P. To connect to a replica set Primary use the mongo shell --host option: mongo --host replicaSetName/host1[:porthost1],host2[:porthost1],host3[:porthost3],etc For example: $ mongo --host rs1/john.local:27019,john.local:27018 MongoDB shell version: v3.4.9 connecting to: mongodb://john.local:27019,john

Printing Mongo query output to a file while in the mongo shell

二次信任 提交于 2019-11-28 19:06:53
2 days old with Mongo and I have a SQL background so bear with me. As with mysql, it is very convenient to be in the MySQL command line and output the results of a query to a file on the machine. I am trying to understand how I can do the same with Mongo, while being in the shell I can easily get the output of a query I want by being outside of the shell and executing the following command: mongo localhost:27017/dbname --eval "printjson(db.collectionName.findOne())" >> sample.json The above way is fine, but it requires me to exit the mongo shell or open a new terminal tab to execute this

How do you connect to a replicaset from a MongoDB shell?

妖精的绣舞 提交于 2019-11-28 15:56:02
问题 If I'm writing an application which connects to mongodb then I can provide a seed list for a replicaset, and the driver will direct me to the master node, where I can run write commands. How do I specify the seed list for a commandline mongo shell in order to conenct to a replicaset. 回答1: To connect to a replica set Primary use the mongo shell --host option: mongo --host replicaSetName/host1[:porthost1],host2[:porthost1],host3[:porthost3],etc For example: $ mongo --host rs1/john.local:27019

how can I connect to a remote mongo server from Mac OS terminal

Deadly 提交于 2019-11-28 15:42:57
问题 I would like to drop into the mongo shell in the terminal on my MacBook. However, I'm interested in connecting to a Mongo instance that is running in the cloud (compose.io instance via Heroku addon). I have the name, password, host, port, and database name from the MongoDB URI: mongodb://username:password@somewhere.mongolayer.com:10011/my_database I have installed mongodb on my MacBook using Homebrew not because I want Mongo running on my Mac, but just to get access to the mongo shell program

How to aggregate by floor in MongoDB

旧时模样 提交于 2019-11-28 14:04:56
MongoDB Aggregation Framework doesn't have floor function. It only has simple arithmetic operators . So, how to compose floor function using them? According to definition floor(number) = number - (number % step) we can compose our aggregation formula: {$subtract: ["$number", {$mod: ["$number", <step>]}]} where step is order of magnitude. First, it computes remainder with $mod and then it computes difference with $subtract . So, grouping Users by age floored to whole numbers will be db.users.aggregate( {$group: {_id: {$subtract: ["$age", {$mod: ["$age", 1]}] }}} ) If you want to floor to 10s or

Pretty print in MongoDB shell as default

旧巷老猫 提交于 2019-11-28 02:32:13
Is there a way to tell Mongo to pretty print output? Currently, everything is output to a single line and it's difficult to read, especially with nested arrays and documents. ( note: this is answer to original version of the question, which did not have requirements for "default" ) You can ask it to be pretty. db.collection.find().pretty() staackuser2 You can add DBQuery.prototype._prettyShell = true to your file in $HOME/.mongorc.js to enable pretty print globally by default. Bhanu Chawla (note: this is answer to the updated question) You can just do this on the CLI: echo DBQuery.prototype.

Loop through all Mongo collections and execute query

孤街醉人 提交于 2019-11-27 23:31:47
First of, I'm quite new to mongodb. Here's my question I've not been able to find a solution to. Let's say I have 3 different collections. mongos> show collections collectionA collectionB collectionC I want to create a script that iterates over all collections ind this database and find the last inserted timestamp in each of these collections. Here's what works inside mongos. var last_element = db.collectionA.find().sort({_id:-1}).limit(1); printjson(last_element.next()._id.getTimestamp()); ISODate("2014-08-28T06:45:47Z") 1. Problem (Iterate over all collections) Is there any possibility to to

How to list all collections in the mongo shell?

蓝咒 提交于 2019-11-27 19:07:53
问题 In the MongoDB shell, how do I list all collections for the current database that I'm using? 回答1: You can do... JS (shell): db.getCollectionNames() node.js: db.listCollections() non-JS (shell only): show collections The reason I call that non-JS is because: $ mongo prodmongo/app --eval "show collections" MongoDB shell version: 3.2.10 connecting to: prodmongo/app 2016-10-26T19:34:34.886-0400 E QUERY [thread1] SyntaxError: missing ; before statement @(shell eval):1:5 $ mongo prodmongo/app -