Update in forEach on mongodb shell

To get what you want you will need a few things: t.forEach(function( aRow ) { var newFields = []; aRow.fields.forEach( function( aField ){ var newItems = []; aField.items.forEach( function( item ){ var aNewItem = { item: parseInt(item), ref: 0 }; newItems.push( aNewItem ); } ); newFields.push({ _id: aField._id, items: newItems }); } ) aTable.update( { … Read more

Loop through all Mongo collections and execute query

There is the db.getCollectionNames() helper method that does this for you. You can then implement your code: db.getCollectionNames().forEach(function(collname) { // find the last item in a collection var last_element = db[collname].find().sort({_id:-1}).limit(1); // check that it’s not empty if (last_element.hasNext()) { // print its timestamp printjson(last_element.next()._id.getTimestamp()); } }) You probably also want a .hasNext() check in … Read more

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

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.local:27018/?replicaSet=rs1 2017-10-12T14:13:03.094+0000 I NETWORK [thread1] Starting new replica set monitor for rs1/john.local:27019,john.local:27018 2017-10-12T14:13:03.096+0000 I NETWORK [thread1] Successfully connected to john.local:27019 (1 connections now open to john.local:27019 with … Read more

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

You are probably connecting fine but don’t have sufficient privileges to run show dbs. You don’t need to run the db.auth if you pass the auth in the command line: mongo somewhere.mongolayer.com:10011/my_database -u username -p password Once you connect are you able to see collections? > show collections If so all is well and you … Read more