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

How to get env from mongosh shell

I need to get an environment variable from mongosh. In the old mongo shell I could do it with the _getEnv method like this _getEnv("MONGO_INITDB_DATABASE"). That shell is removed from MongoDB 6.0. So I can only use mongosh shell in mongo 6.0 and…
Martin Kroll
  • 169
  • 7
2
votes
0 answers

Stop Pretty default Mongo Shell (mongosh)

I jsut installed the new version, which is mongosh, and pretty printing is by default. When I am looking at multiple records, I find it inconvenient. Does anyone know how to turn off pretty print by default in mongosh …or at all? I didn’t know about…
Adnan khan
  • 21
  • 2
2
votes
1 answer

Mongodb, update part of string with regex in one query

How to substitute part of string in mongodb with update query. Example, I have this: _id: 123 url:"test.com?from_user=1" name:"Superhero" _id:124 url:"regex.com?from_user=3" name:"Journalist" _id:123 url:"test.com?from_user=2" name:"Software…
bekanur98
  • 502
  • 1
  • 7
  • 20
2
votes
1 answer

Calculate values dynamically when updating a document in mongosh

I'm trying to update some documents using the mongo shell in mongodb compass. I'm using the updateMany function to do so. I want to calculate some values when updating the field. for example This is an example document Account capital: 100, …
2
votes
1 answer

Can't start mongodb-community - error 12288

I was able to start mongodb-community server 2 days ago but now I cannot. I do not remember making any changes on it. At some point I have changed my node version to an old one but changed it back to newer one already. So I don't think that should…
Sam Brown
  • 21
  • 3
2
votes
1 answer

Problem using any type of queries on MongoDB Compass

I just started using MongoDB and everything that goes with it. Whenever I enter a query in Mongosh, I either get no output or this error : "Error: clone(t={}){const r=t.loc||{};return e({loc:new Position("line"in r?r.line:this.loc.line,"column"in…
Arkuto
  • 21
  • 1
2
votes
2 answers

how can I modify a field name / key in a nested array of objects in mongodb?

I have a mongodb collection with a number of objects like this: { "_id" : "1234", "type" : "automatic", "subtypes" : [ { "_id" : "dfgd", "name" : "test subtype", "subjetRequired" : true, }, { "_id" : "dfgd", …
mal
  • 3,022
  • 5
  • 32
  • 62
2
votes
1 answer

How to save a query from model.findOne() on a variable using mongoose

I am trying to save the results from model.findOne() on a variable for using the results on another function. But what I get on the variable is only a pending promise: let key = Product.findOne({ name: 'keyboard' }).then(data => { return data…
user19359397
2
votes
3 answers

Is there a way to standardize mongosh JSON output?

MongoDB upgraded their command-line interface (formerly mongo, now mongosh). Before, the mongo command output was in proper JSON, the kind jq could ingest. For example: { "abc" : "hello" } The new mongosh command now outputs stuff like: { abc:…
2
votes
1 answer

How to specify a database on a Linux mongosh command line along with authentication

I have a somewhat annoying issue that ought to be trivial but I cannot figure it out. I am running on a Ubuntu Linux system (Focal); I have a mongo (version 5.0.3) database with authentication enabled. I cannot find a command line that will drop…
One Guy Hacking
  • 1,176
  • 11
  • 16
2
votes
1 answer

Is there any way to limit user resources in MongoDB?

Can I limit user resources in MongoDB? Example of this limitation in MySQL is to limit queries per hour or connections per hour for a user. I am using mongosh to interact with MongoDB version 5.0.2
Unsel
  • 343
  • 2
  • 8
2
votes
2 answers

Mongo Shell update nested array of documents

I try to set an attribute of an document inside an array to uppercase. Here is the link to the mongodb playground https://mongoplayground.net/p/BTP_h3kqK_S this is a document example { "_id": ObjectId("5e786a078bc3b3333627341e"), "value":…
Dimi
  • 23
  • 5
2
votes
0 answers

Is it possible to connect via mongo shell in read-only mode?

Is there any way to connect to MongoDB using mongo shell with read-only mode? $ mongo "mongodb://mongodb0.example.com:27017/?readOnly=true" I know I can create read only user then connect to DB.
Gagan
  • 1,267
  • 3
  • 18
  • 28
2
votes
1 answer

How to get only part of document in MongoDB with Node.js?

"Geography": { "Tourism": { "B001": { "code": "B001", "max": " 140", "min": " 97", "minWithQuota": " 91" } } }, "MathAndPhysic": { "IT": { "B183": { "code": "B183", "max": "140", "min": "…
2
votes
1 answer

How do I use find () in mongodb with a conditional to use one field or another?

I have to return documents that are between two dateTime values ​​or if the dateTime field does not exist, use the createdAt field. I'm using this query: { dateTime: { $gte: 1554087600000, $lte: 1609431033000 } } But sometimes the dateTime field…