Questions tagged [mongo-shell]

The mongo shell is an interactive JavaScript shell for MongoDB, and is part of all MongoDB distributions. This section provides an introduction to the shell, and outlines key functions, operations, and use of the mongo shell.

Most examples in the MongoDB Manual use the mongo shell; however, many drivers provide similar interfaces to MongoDB.

FAQ

713 questions
6
votes
3 answers

add property to nested array in mongodb document

I have a mongodb document with the following structure > db.user.find().limit(1); { "_id" : "1", "foo" : { "bars" : [ { "name" : "bar1" }, { "name" : "bar2" }, ], ... }, ... } I want to add a new property to each…
markdsievers
  • 7,151
  • 11
  • 51
  • 83
6
votes
1 answer

How does javascript forEach() function iterate documents in mongodb shell?

How does javascript forEach() function work in mongodb shell? With what sequence are the documents being processed? Eg, let's say that I have two big collections and those two collections should contain the same number of documents (note that the…
Dimitrios
  • 445
  • 1
  • 4
  • 14
6
votes
1 answer

How to run a local script in mongo shell - Solution load()

I think this is a very basic question but I'm stuck. I'm connected to remote mongo instance (mLab) via a MongoDB shell. This has been fine for one-liners, but now I want to run larger commands but often, thus the need to do it from an already…
Steve Taylor
  • 412
  • 1
  • 4
  • 11
6
votes
0 answers

mongo shell cat() file to big to load as a variable at

I have javascript that I load to mongo to get some info. mongo --port=27017 script.js I am passing ObjectIds from the file as a variable: #script.js db=connect('127.0.0.1:27017/dbname') db.auth('user', 'pass') //get credentials var file =…
oshaiken
  • 2,593
  • 1
  • 15
  • 25
6
votes
1 answer

Why cant I connect to replica set using mongo shell?

I can connect to replica set using following command from mongo shell version 3.2.11. But the same does not works with mongo shell v3.4.1. mongo --host \ "replicaSet1/10.10.10.15:27001,10.10.10.16:27002,10.10.10.17:27000" mydbname \ -u…
Nilesh
  • 2,089
  • 3
  • 29
  • 53
6
votes
1 answer

Is it possible to create ObjectId in Mongo shell?

I'm updating a document with an array of embedded documents, directly in Mongo shell. I would like each of these sub-docs to have an _id field, but these are not created automatically as they are for top-level documents. Is there a way to simply…
Antoine
  • 5,055
  • 11
  • 54
  • 82
6
votes
2 answers

How can I print document in cursor.ForEach?

I've defined some simple cursor as find() on some collection. cursor = db.students.find() Student has following schema: "_id" : 19, "name" : "Gisela Levin", "scores" : [ { "type" : "exam", "score" :…
Arkadiusz Kałkus
  • 17,101
  • 19
  • 69
  • 108
6
votes
1 answer

issue in run mongostat for auth enabled mongodb

I just implemented auth for mongodb, There is a user ‘admin’ in db ‘admin’ and ‘appadmin’ in db ‘mydb’ all working fine. below are my db auth settings: use admin db.auth(‘’, ‘’) db.getUsers() [ { "_id" : "admin.admin", "user" :…
Jisson
  • 3,566
  • 8
  • 38
  • 71
6
votes
2 answers

Is there a findById shortcut for the MongoDB shell?

The most common thing I do in the mongo DB shell is find objects by ID, eg: db.collection.find({_id: ObjectId("55a3e051dc75954f0f37c2f2"}) I do this over and over and I find having to wrap the id with ObjectId over and over gets old. I wish I had a…
Andrew Lavers
  • 8,023
  • 1
  • 33
  • 50
6
votes
2 answers

How to define aliases in the MongoDB Shell?

I'm using the MongoDB shell and like to define some shortcuts. For example, it would be nice to abbreviate show databases with sd. I already managed to add a function hw() to the MongoDB shell by adding it's definition to the ~/.mongorc.js: function…
Edward
  • 4,453
  • 8
  • 44
  • 82
6
votes
1 answer

Order of key values in mongoDb

mongo code : db.temperature.insert({"x":3,"y":4}); db.temperature.find(); OUTPUT { "_id" : ObjectId("52b418fb132c1f3236831447"), "y" : 4, "x" : 3 } Please help me to understand why in my case(above.) The find method is showing Y value first and x…
Yashodhan K
  • 160
  • 1
  • 9
6
votes
1 answer

mongodb- connecting to test hanging and mongo shell not starting

I'm trying to run mongodb (2.2.6 shell) on newly installed windows7 OS and cygwin. -I downloaded 2.2.6 version from mongodb website, unzipped it in C: and created folder C:\data\db with write permissions. Now, I start cygwin and CD'ed to bin folder…
rVr
  • 1,331
  • 8
  • 11
6
votes
1 answer

Why MongoDB shell new ISODate(0001-01-01) returns date 1901-01-01

In MongoDB Shell on windows if you run a query with a value of new ISODate('0001-01-01T00:00:00Z') it actually seems to search for new ISODate('1901-01-01T00:00:00Z') If you enter "new ISODate('0001-01-01T00:00:00Z')" directly in the Mongo Shell…
Mr Grok
  • 3,876
  • 5
  • 31
  • 40
6
votes
2 answers

How do I add another value to a dictionary in MongoDB?

Here's an example of what I'm trying to do, as I can't explain what I mean. I execute this in the mongo CLI: db.posts.insert({name: "Hello, world!, body: "Here's my first blog post. Happy reading!", comments: []}). What I want to do is update that…
user775171
5
votes
2 answers

How can I get mongo db actual size?

When I run below command in mongo shell, db.stats() I get below and this is wired. When I check mongoDB installation folder it only used around 2 GB disk space? see below fsTotalSize over 80 GB
Pradeep Asanka
  • 403
  • 1
  • 5
  • 11