Questions tagged [mongodb-shell]

The MongoDB shell is a command-line tool for querying and administering MongoDB.

The MongoDB shell is a command-line tool for MongoDB. It allows for querying collections, as well as admin functions like adding users, copying databases, and profiling.

Mongo Command Line

http://cheat.errtheblog.com/s/mongo

115 questions
1
vote
0 answers

How to connect from MongoDB Atlas version 4.4 to Mongo DB Shell?

I copied the Mongo DB Shell connection string from Mongo DB Atlas and pasted to Mongo DB Shell. I got the following error. Please enter a MongoDB connection string (Default: mongodb://localhost/): mongo…
1
vote
2 answers

mongodb shell login - password contains special characters like -(hyphen) and '(single quote)

I am trying to login to mongodb database using mongodb shell, and if the password contains any special characters like -(hyphen) or '(single quote) , it gives error - Error parsing command line: unrecognised option '-8B'df5='. mongo -u username -p…
Smith
  • 151
  • 3
  • 11
1
vote
1 answer

Shell logging for mongodb

I am new to mongodb and trying to figure out a lot. Can we configure shell logging for mongodb? What I mean is the capability to get logs from the Shell to track queries for auditing purposes for version 4? If so - what commands do we have to run…
Nchang
  • 11
  • 1
1
vote
1 answer

$addFields and calculation not working - mongodb shell

db.Hours.aggregate( {$addFields: {TrueAmbientTemp: { {$add : [-8 , {$multiply : ["$AmbientTemp" , 47]}]}}}} ) I am trying to add a new field TrueAmbientTemp which is a calculation field. The above give an error.
1
vote
1 answer

Struggling to convert zonedatetime string to date with mongodb shell

I have these mongo documents with date like this: this date is java: ZonedDateTime.now(), but it came into mongo as string: db.test.insert({"dateString" : "2020-07-24T11:06:53.975+02:00[GMT+02:00]"}) So i want to pull a report, but formatted it…
Mosd
  • 1,640
  • 19
  • 22
1
vote
1 answer

mongodb shell require a js function from another file

I have two Mongodb shell scripts written in js. Both are sharing some code between them. Is it possible to extract that code to another file and to import it from there? And how? I have been trying with require / import but that obviously not…
U Rogel
  • 1,893
  • 15
  • 30
1
vote
0 answers

MongoDB v4 explain() doesn't return number of scanned docs (unlike the previous version). How to be?

I am reading the book "MongoDB in Action" and there is an example of the explain() method. Shell command: db.values.find({}).sort({close: -1}).limit(1).explain() Result: db.values.find({}).sort({close: -1}).limit(1).explain() { "cursor" :…
1
vote
1 answer

What is the difference between "connect with compass" and "connect with mongo shell" in mongodb?

I've been trying to follow a tutorial of How to connect Flutter and MongoDB where he connects MongoDB with Terminal (Command Prompt). For some reason I couldn't connect with MongoDB using Shell, So I thought if I could connect MongoDB using Mongo…
1
vote
1 answer

How to query greater than values in different documents from same collection in mongodb?

I have a sales collection as following. I want to query using mongo shell to find product details which cost greater than 20. [{ "Customer_Info":{ "Customer_Id": 1, "First_Name": "John", "Last_Name": "Smith", "Address":…
Nafi Pantha
  • 169
  • 1
  • 3
  • 16
1
vote
1 answer

How to change the mongod db query to aggregation

I have three queries and now I want to change this to MongoDB aggregation and below is the code: db.mycollection.count({$and:[$requestB:{$exists:false},[$requestC:{$exists:false}]}) db.mycollection.count({requestB:{$exists:true}}) …
Jay Park
  • 308
  • 1
  • 6
  • 14
1
vote
2 answers

How to convert a sting field to an ObjectId inside of an array of of objects via MongoDb Shell?

So my MongoDb doc looks like this: { "_id" : ObjectId("5f1842d238ec58056d94bbb3"), "itemsPurchased" : [ { "_id" : "5e7365360665d10011756af2", "created" : "2020-03-19T12:27:34.108Z", "productLine"…
taz_
  • 137
  • 3
  • 13
1
vote
2 answers

Abort mongo shell command (as a infinite loop) without ending the shell itself

I run the following in MongoDB shell (version 4.2, as reference): while(true) { db.c.update({_id:1}, {$inc: {n: 1}}) } I want to break the while loop, but without ending the MongoDB shell. If I use Ctrl+C the loop ends, but also the mongo process…
fgalan
  • 11,732
  • 9
  • 46
  • 89
1
vote
1 answer

Allowed to connect without authentication and read only users can write in MongoDB

I had to create a new user in a MongoDB Server and grant it read-only permissions to a single database. Note that I am not the administrator here. I connected with my user and executed the following code: db.createUser({user: "newuser", pwd:…
Victor Ferreira
  • 6,151
  • 13
  • 64
  • 120
1
vote
1 answer

I got error in mongodb shell while redeclaration

Any faced this issue in mongodbshell let martian = db.movieDetails.find({"title":"The Martian"}) let martian = db.movieDetails.find({"title":"The Martian"}) 2020-04-20T12:54:11.297+0400 E QUERY [js] uncaught exception: SyntaxError: redeclaration…
1
vote
0 answers

MongoDB JSON Validation did not pass

I created a very basic counter collection with a validator. I also put a method to call it. In the method, I have a findAndModify function to update sequence. When I call it it doesn't pass the validator only if I change its level as MODERATE. What…