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
21
votes
2 answers

MongoInvalidArgumentError: Update document requires atomic operators

I am going through a course on MongoDB. Below is my list of documents in a collection called 'flightData'. Below is result of find query: flights> db.flightData.find() [ { _id: ObjectId("611aaa1c4a0269583c8d81b0"), aircraft: 'AirBus…
Chandan Kumar
  • 303
  • 1
  • 3
  • 11
21
votes
12 answers

How to copy and paste text in MongoDB shell

How to copy and paste text in MongoDB shell? I tried Ctrl+C and Ctrl+V but it didn't work. Thanks, Michael.
Michael Horojanski
  • 4,543
  • 4
  • 33
  • 34
21
votes
1 answer

Iterate over all Mongo database

I'm relatively new to MongoDB and I've not been able to find a solution for what I'm looking for. I would like to iterate over all mongo databases and run some command on each collection of each database. I can run the following command to get all…
Bakul G
  • 213
  • 1
  • 2
  • 5
21
votes
1 answer

Cannot use commands write mode error, Degrading to compatibility mode

I just play with mongo shell and came across with Cannot use commands write mode, degrading to compatibility mode. I connected to remote mongo server (mongolab) and tried to insert new record to collection by my simple script: // script.js db =…
NHG
  • 5,807
  • 6
  • 34
  • 45
15
votes
6 answers

How do I clear the command history in the mongo shell

Is there a command to clear the history from within the mongo shell?
ABCoder
  • 296
  • 3
  • 11
15
votes
1 answer

can't make basic mongo shell script with authentication

I have a really complicated issue that i think i can solve by writing a mongo shell script but i can't even manage to make a simple connection. I have a local mongo database which is requires a username/password that i normally access like…
Dallas Caley
  • 5,367
  • 6
  • 40
  • 69
14
votes
2 answers

query in mongo Shell gives SyntaxError: missing : after property

db.movieDetails.find( { year: 2013, imdb.rating: Pg-13, award.wins: 0 }, { title: 1, _id: 0 } ).pretty(); The mongo shell returns this error 2016-08-13T09:08:00.648+0200 E QUERY [thread1] SyntaxError: missing : after property id…
Computerlucaworld
  • 387
  • 1
  • 2
  • 14
13
votes
3 answers

Cannot connect to mongodb atlas through mongo shell

I've checked other similar posts but none worked for my case. I upgraded my mongo shell right before connecting to MongoDB Atlas, from which I created a free sandbox database. I used this following for my connection, which was given by atlas. mongo…
Darius
  • 441
  • 1
  • 5
  • 13
12
votes
1 answer

Using MongoDB C# Driver write ElementMatch with Regex query

I need to construct the following query using MongoDB C# driver db.Notes.find({ "Group._id" : 74, "CustomFields" : { "$elemMatch" : { "Value" : /batch/i } }, "IsDeleted" : false }).sort({ "CreatedDateTimeUtc" : -1 }) I used a query like this…
HaBo
  • 13,999
  • 36
  • 114
  • 206
12
votes
2 answers

Is it 100 million documents too much?

Well, I am new to mongo and today morning I had a (bad) idea. I was playing around with indexes from the shell and decided to create a large collection with many documents (100 million). So I executed the following command: for (i = 1; i <= 100;…
chaliasos
  • 9,659
  • 7
  • 50
  • 87
11
votes
2 answers

Adding new property to each document in a large collection

Using the mongodb shell, I'm trying to add a new property to each document in a large collection. The collection (Listing) has an existing property called Address. I'm simply trying to add a new property called LowerCaseAddress which can be used for…
Justin
  • 17,670
  • 38
  • 132
  • 201
11
votes
3 answers

variables not set in mongo shell

I am trying to work with mongo shell and I am having challenges in storing the value inside of mongo shell when I find a user in the document users, i store it in the variable doc > var doc = db.users.find({"username":"anil"}); When I type doc in…
user641887
  • 1,506
  • 3
  • 32
  • 50
11
votes
4 answers

Saving the result of a MongoDB query

When doing a research in mongo shell I often write quite complex queries and want the result to be stored in other collection. I know the way to do it with .forEach(): db.documents.find(query).forEach(function(d){db.results.insert(d)}) But it's kind…
vorou
  • 1,869
  • 3
  • 18
  • 35
10
votes
1 answer

$lookup with same collection

I am new to MongoDB so I am not sure if I have phrased my question correctly. I have a collection in which data looks like this: { "_id" : ObjectId("66666"), "Id" : 994, "PostType" : 1, "AnswerId" : 334, "CreationDate" :…
10
votes
5 answers

How to use a for loop in the mongodb shell?

How can i use a for loop in the mongo db shell? My attemps are stucking at this point: for (var i = 0; i <= 6; i=i+0.12){ var n = i + 0.12; db.test.aggregate( { $sort: {'deviation': -1}}, { $unwind: '$foo' }, { $match: { 'foo.km': {$gt:…
1
2
3
47 48