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
898
votes
23 answers

How can I list all collections in the MongoDB shell?

In the MongoDB shell, how do I list all collections for the current database that I'm using?
coffee-grinder
  • 26,940
  • 19
  • 56
  • 82
572
votes
8 answers

Pretty print in MongoDB shell as default

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.
raffian
  • 31,267
  • 26
  • 103
  • 174
142
votes
7 answers

mongorestore error: Don't know what to do with the dump file

I have mongo DB installed in the following path c:\mongodb\bin. I have configured my environment variable PATH in advanced settings.I also have mongod running .When I run the following command mongorestore dump from the following path c:\hw1-1\dump…
Prasanna Aarthi
  • 3,439
  • 4
  • 26
  • 48
116
votes
3 answers

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

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…
tadasajon
  • 14,276
  • 29
  • 92
  • 144
112
votes
11 answers

How to replace substring in mongodb document

I have a lot of mongodb documents in a collection of the form: { .... "URL":"www.abc.com/helloWorldt/..." ..... } I want to replace helloWorldt with helloWorld to get: { .... "URL":"www.abc.com/helloWorld/..." ..... } How can I achieve this for…
user1071979
  • 1,701
  • 2
  • 12
  • 25
104
votes
8 answers

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

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…
Parijat Kalia
  • 4,929
  • 10
  • 50
  • 77
83
votes
10 answers

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

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…
Richard Warburton
  • 1,462
  • 1
  • 9
  • 13
83
votes
2 answers

How to clear console in MongoDB

While using Mongodb console or shell sometimes my screen is cluttered with a lot of previous output which I do not need. So, is there a function to clear console in MongoDB? Something like an analog of clear in MatLab. I have tried clc, clear but…
user1015104
51
votes
4 answers

How to list all users in the mongo shell?

In the MongoDB shell, how do I list all users for the current database that I'm using? I can't seem to find it anywhere on stackoverflow.
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
34
votes
2 answers

Loop through all Mongo collections and execute query

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…
cb0
  • 8,415
  • 9
  • 52
  • 80
32
votes
3 answers

Update in forEach on mongodb shell

I have got a collection aTable with 2 records: { "title" : "record 1", "fields" : [ { "_id" : 1, "items" : [ 1 ] }, { "_id" : 2, "items" :…
Mr Jerry
  • 1,686
  • 3
  • 14
  • 22
29
votes
4 answers

Update query in MongoDB shell

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…
Shamoon
  • 41,293
  • 91
  • 306
  • 570
28
votes
5 answers

Mongo shell execute query from file and show result

How to execute external file using mongo shell and see the result in console? I have external file, like query.js and I would like to execute it and see the result in cmd. Let's say, content of the file is: db.users.find()
Andrei
  • 42,814
  • 35
  • 154
  • 218
25
votes
1 answer

How to retrieve the documents whose values ending with a particular character in mongoDB

I inserted the documents into a particular database in mongoDB. for example db.employee.insert({name:"nithin",age:22}) db.employee.insert({name:"sreedevi",age:32}) now i wnat to retrive the documents whose name ending with character 'i'.
Nithin K Anil
  • 263
  • 1
  • 3
  • 6
24
votes
2 answers

Specify which database to use in mongodb .js script

I just want to run a .js script against a particular mongodb database, but can't seem to get the correct syntax. echo "print(db.address.find().limit(1));" > test.js mongo test.js How do I select the database that this will be executed on, I've…
markdsievers
  • 7,151
  • 11
  • 51
  • 83
1
2 3
47 48