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
4
votes
1 answer

How to reference a collection name with "." in Mongo db?

I have a mongo DB, in the mongo shell, when I run "show collections", it returns the collection with dot in the collection name like a.1 a.2 a.3 How I refer it in command like db.a.1.find() the above returns SyntaxError: missing ; before statement…
Russel Yang
  • 2,633
  • 3
  • 21
  • 18
3
votes
1 answer

How to update nested documents in an array in mongodb document without using positional operator $?

I have an issue in which I am not able to use the positional operator to update subdocuments because I am using MongoDB version 3.2 which doesn't support the positional operator $, and I am not able to upgrade MongoDB version to more than 3.2 due to…
hamza
  • 65
  • 8
3
votes
3 answers

MongoServerSelectionError: connection to closed

I have started learning MongoDB, I am just trying to connect to the database using MongoDB Shell. I am using the below command. mongosh "mongodb+srv://cluster0.12345.mongodb.net/myFirstDatabase" --username viveknuna I have added my current IP…
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
3
votes
1 answer

How can I get a non-truncated list of distinct values using the new MongoDB Shell (mongosh)

I was doing some one-time manual analysis of my data using the new MongoDB shell (mongosh) to query the collections directly. One thing I needed was a list of distinct field values from a collection: > db.myCollection.distinct("myExternalId") [ …
M. Justin
  • 14,487
  • 7
  • 91
  • 130
3
votes
4 answers

Pipe output from mongosh MongoDB Shell to output file (Windows)

I am using the current mongosh tool to run Mongo queries which may return very large result sets, which might not fit within the buffer of the command prompt (and even if they did, copying such results by hand would be time consuming and error…
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
3
votes
1 answer

mongodb replica set error "...replSetHeartbeat requires authentication..." after running rs.initiate()

I have 2 virtual machines with mongodb running on both. I have created a user and roles and added data for testing. Everything works until I try to set up a Replica set. When I run rs.initiate() I get the "...replSetHeartbeat requires…
James Morris
  • 353
  • 5
  • 20
3
votes
1 answer

How to mongo shell in quiet mode with a replica set

When I try to execute a JavaScript code using --eval, mongo shell generates extra output, --quiet option doesn't seem to work check_mongodb_availability() { local hostname=$1 local database=$2 local js=" var found = 0; var dbs =…
Mahela Wickramasekara
  • 603
  • 1
  • 11
  • 22
3
votes
1 answer

Creating a user for a non existing database not working

I am trying to deploy a MongoDB database in a docker container. This database is then used by a node js server. When I start the mongo container, it first executes a shell script that works because I can see it log the error code 1 which means the…
Jaro
  • 1,587
  • 5
  • 20
  • 39
3
votes
1 answer

Is using a newer Mongo shell version with an older MongoDB version safe?

I use MongoDB on multiple production servers, with different versions for different projects. My development environment has v3.6 of the shell but when I connect to a server with MongoDB v3.4, I am greeted with this message upon login: WARNING:…
TheKarateKid
  • 772
  • 11
  • 19
3
votes
1 answer

cannot use the part (...) to traverse the element

Running mongod 3.6 and attempting to use this example. Here is the sample data: > db.students2.find().pretty() { "_id" : 1, "grades" : [ { "grade" : 80, "mean" : 75, "std" : 8 }, { …
Bennett
  • 1,007
  • 4
  • 15
  • 29
3
votes
2 answers

mongodb populate flat file results?

I am trying to remove old files from the file system The file list is maintained in mongoDb collection like so: { "_id" : ObjectId("59a39215953f77968789d692"), "className" : "com.xerox.model.ModuleDocument", "documentId" : "0000001643", …
JavaSheriff
  • 7,074
  • 20
  • 89
  • 159
3
votes
1 answer

How to suppress the output of the "switched to db" message when running javascript via the mongo shell?

When running a javascript file using the mongoshell the program prints out some information which is expected. However at the top of the screen the first line says "switched to db foo" resulting from the "use foo" command in the program. use foo; …
ciso
  • 2,887
  • 6
  • 33
  • 58
3
votes
1 answer

Mongo UUID Type 03 instead of 04 from mongo shell

MongoDB UUID conversion from Mongo Shell turns out be TYPE 03, legacy format. How do we get it to work with TYPE 04, binary format? > db.foo.insert({"_id":1000,"key1":UUID("240003A09CEC456AB57B98FF8E0E45DB")}) WriteResult({ "nInserted" : 1 }) >…
dilsingi
  • 2,938
  • 14
  • 24
3
votes
1 answer

Unordered bulk update records in MongoDB shell

I've got a collection consisting of millions of documents that resemble the following: { _id: ObjectId('...'), value: "0.53" combo: [ { h: 0, v: "0.42" }, { h: 1, v:…
RizJa
  • 1,961
  • 1
  • 21
  • 38
3
votes
1 answer

How to display command output page per page in mongo shell

db.serverStatus() returned many running pages in mongo shell on my Windows 10 machine. What is the command to view the result page by page? less , tail -n or man did not work inside the shell.
Treefish Zhang
  • 1,131
  • 1
  • 15
  • 27