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 generate a unique ObjectId inside an update or aggregation?

Some sub documents in my collection are missing _ids. I've written this piece of mongo-shell code to try to add them. db.jobs.updateMany({}, { $set: { "artifacts.$[elem]._id": new ObjectId() } }, { arrayFilters: [ { "elem._id":…
Brian Drew
  • 43
  • 4
4
votes
1 answer

command failed: MongoError: CMD_NOT_ALLOWED: profile

I am using the Atlas free cluster mongodb and try to enable the profiler for the monitoring purpose and I have an Atlas Admin access on the cluster On the setting, the profiler option is enabled as in the diagram above While I am running the…
San Jaisy
  • 15,327
  • 34
  • 171
  • 290
4
votes
1 answer

MongoDB: Get only specific fields in nested array documents

I'm trying to project the values from nested array documents which are in the below format. What I expect is to display only the specValue of the specific specType selected in the find query. { "carId": "345677" "car" : { "model" :…
coderpc
  • 4,119
  • 6
  • 51
  • 93
4
votes
1 answer

MongoDB text index search

I had created a collection in mongo db as show below db.articles.insert([ { _id: 1, subject: "one", author: "abc", views: 50 }, { _id: 2, subject: "lastone", author: "abc", views: 5 }, { _id: 3, subject: "firstone", author: "abc", views: 90 }, …
4
votes
1 answer

RoboMongo can't use array filters?

Following on from "How to update collection and increment hours for ISO date". It seem that RoboMongo can't execute a query with an array filter: This same query if find when executed through the Mongo shell though: > db.collection.update({ ... …
BanksySan
  • 27,362
  • 33
  • 117
  • 216
4
votes
3 answers

Delete documents by date comparison between sum of date and numeric fields and current timestamp

The documents in the MongoDB collection have the following schema (showing only relevant fields): { "TTLinSeconds" : 1800, "lastUpdatedTimestamp" : ISODate("...") } I need to delete all documents where current timestamp is greater than…
Jacek
  • 194
  • 1
  • 11
4
votes
2 answers

MongoDB aggregation Shell script to MongoC# Driver

How can I convert this Mongo Shell script to MongoDB C# Driver? var myItems = [] var myCursor = db.Tickets.aggregate( [ { $match : { TicketProjectID : 49 } }, { $project: { TicketProjectID:1, TicketID:1, concatValue: { $concat: […
HaBo
  • 13,999
  • 36
  • 114
  • 206
4
votes
3 answers

MongoDB - change simple field into an object

In MongoDB, I want to change the structure of my documents from: { discount: 10, discountType: "AMOUNT" } to: { discount: { value: 10, type: "AMOUNT" } } so I tried following query in mongo…
Lukasz Wiktor
  • 19,644
  • 5
  • 69
  • 82
4
votes
1 answer

Mongodb multi update, only updates a single document in mongo-shell

I am learning mongodb and am trying to perform a simple multi documents update with the {$multi: true} option, but it only updates a single document. > db.users.update({'color': 'black'}, {$set: {'title': 'blackers'}}, {$multi:…
ArchNoob
  • 3,946
  • 5
  • 32
  • 59
4
votes
3 answers

hex instead of base64 in BinData() - MongoDB

Is there a way to send hex of binary value instead of base64 to BinData() in mongo ?
Hamid Mamdoohi
  • 134
  • 1
  • 9
4
votes
2 answers

MongoDB weird writeResult behaviour

I am using mongodb (v2.6.7) and mongo (2.6.7) shell client. I am trying to use the WriteResult object returned by the insert and and update commands. According to mongodocs in case of error it returns a writeResult object with writeError…
Nitiraj
  • 614
  • 1
  • 4
  • 15
4
votes
2 answers

mongodb printjson will output ObjectId to result, which can not be used for JSON.parse

I run my mongo shell script like this: mongo --quiet myscript.js > /tmp/my.json I use printjson in myscript.js. mongodb printjson will output ObjectId to my.json, like this: "_id" : ObjectId("5444a932ca62bbcba14a1082") I read some source code from…
lutaoact
  • 4,149
  • 6
  • 29
  • 41
4
votes
2 answers

MongoDB: find execution time for count() command on millions of records in a collection?

I am trying to find time required to perform a count() on a collection which is consisting of millions of testdata records, with following scenario:- 1) From 1st Mongo shell I am inserting millions of records into collection using a code for (var i…
Swapnil Sawant
  • 620
  • 8
  • 21
4
votes
1 answer

What does the "it" stand for in the MongoDB shell?

You see it when the results of a find are too long to display in the terminal. Type "it" for more There's a good chance "it" just stands for it, but I'm not certain and I can't seem to find any documentation on it. For all I know, "it" could also…
artburkart
  • 1,830
  • 1
  • 20
  • 28
4
votes
2 answers

MongoDb: How to drop a database using shell using authentication?

I have a authenticated user with all required privileges to drop a database. I want to drop the database from the shell and tried following command mongo -uuser -ppass newdb --eval "db.dropDatabase();" I got following:- MongoDB shell version:…
bor
  • 2,211
  • 4
  • 22
  • 37