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.
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…
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…
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…
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 =…
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…
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…
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" :…
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" :…
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…
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…
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…
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…
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…
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…
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