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
5
votes
3 answers

MongoDB convert string type to float type

Following the suggestions over here MongoDB: How to change the type of a field? I tried to update my collection to change the type of field and its value. Here is the update query db.MyCollection.find({"ProjectID" : 44, "Cost": {$exists:…
HaBo
  • 13,999
  • 36
  • 114
  • 206
5
votes
1 answer

MongoDb explain failed: "unknown top level operator: $query"

I'm trying to obtain explain from quite simple query. It uses posts collection with following schema: > db.posts.findOne() { "_id" : ObjectId("55236e6182bf196454a952b6"), "Content" : "wuOfCjKborHcxkoyXzXiW", "CreatedAtUtc" :…
Arkadiusz Kałkus
  • 17,101
  • 19
  • 69
  • 108
5
votes
1 answer

mongo shell, how do I direct queries to the secondary

I'm very new to mongo and I'd like to direct some queries to the secondaries from the mongo shell. I've read the docs which say to set the read preference. Specifically In the mongo shell, the readPref() cursor method provides access to read…
mark
  • 1,769
  • 3
  • 19
  • 38
5
votes
4 answers

Not able to use mongo shell in intellij

i am able to connect to mongo db from intellij. Mongo explorer is working fine. but after connecting when i try to open up Mongo Shell on any of the db it is not able to connect. the error is coming on intellij I am running on Windows 7 throwing…
Kishore Tulsiani
  • 1,106
  • 13
  • 29
5
votes
1 answer

mongodb inital load - building DBRef on the fly

I am trying to load initial data to mongodb using db.collections.insert. I have a case where I'll insert the town collection followed by zipcode collection. The zipcode collection refers to town collection. However when I write my script, I do not…
Srik
  • 2,341
  • 2
  • 21
  • 35
5
votes
1 answer

Is there a way to override default functions in Mongodb?

So what I want to do is make findOne work more like it does in Meteor, but through the Mongo shell. In short I want to be able to do something like this db.collection.findOne("thisIsAnId") and have it lookup that id in that collection. I tried…
Shaded
  • 17,276
  • 8
  • 37
  • 62
5
votes
1 answer

Getting an Error trying to add a user to mongodb 2.6.5

I have spent a majority of my day trying to add a user to mongodb 2.6.5. I installed & re-installed Mongo via macports. Try #1 ( addUser(); ): 1. # switching to the admin user > use admin 2. # db.addUser("admin", "password"); When use the…
Ryan
  • 71
  • 2
  • 3
5
votes
1 answer

Does _id field change in MongoDB when copying data from one collection to another?

We are planning on using MongoDB _id as a key that we would provide to the client. Therefore, the requirement is that this key should not change if we ever need to move the data from one collection to another. The copy will be performed using…
arozz90
  • 63
  • 1
  • 5
5
votes
1 answer

db.collection.remove() not removing document

I have a collection TextDocuments /* 0 */ { name:"doc2", pages:[{ pageNumber:"1", text:"This is first page text", },{ pageNumber:"2", text:"This is second page text", },{ pageNumber:"3", text:"This is third page text", }] } /* 1…
Rohit Kumar
  • 1,018
  • 3
  • 12
  • 23
5
votes
1 answer

Can mongo shell warn about operations on non-existing collections?

It happened several times that I made a typo in a collection name or forgot to switch database to that one which includes the desired collection. Mongo shell accepted silently all those commands. Is it possible that mongo shell will warn me if I try…
Lukasz Wiktor
  • 19,644
  • 5
  • 69
  • 82
5
votes
1 answer

Updating int array gets converted to double array in Mongo shell

I have int array to update in collection using mongo shell .When I update it actually it stores in double format . var array =[1,2,3]; // int array as all elements are int // Update query where path is the…
Sumeet Kumar Yadav
  • 11,912
  • 6
  • 43
  • 80
5
votes
1 answer

mongodb querying collection starting with _

Basically the question is simple: How can I issue a query on a collection, which starts with _? For example if I have 2 collections test and _test, and I am trying db.test.findOne() and db._test.findOne() in mongoshell the first one is working as…
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
5
votes
1 answer

MongoDB - How to select only numeric strings / Check whether string is numeric in mongo-shell

I have a collection where the fields are string but those strings can have a numeric value inside e.g.: myObject: { examples: [          {example: "words", ...},          {example: "more words", ...},          {example: "111", ...}, …
Jenninha
  • 1,357
  • 2
  • 20
  • 42
4
votes
1 answer

MongoDB shell collapses nested documents/arrays

The MongoDB Shell (mongosh) collapses nested documents/arrays from a certain nesting depth by default. This makes viewing a document in its entirety impossible. Is there any way to uncollapse these without using an elaborate…
P. Drijkoningen
  • 235
  • 1
  • 7
4
votes
4 answers

timeoutError - MongoServerSelectionError: connect ECONNREFUSED ::1:27017 - [NodeJS + MongoDB]

Disclaimer: Similar topics did not offer a valid solution for my problem! Restarted MongoDB server (it keeps running when error occurs) Using MongoDB server on windows as a service (started it manually) Established the connection via MongoDB Shell…
Jahn E.
  • 1,228
  • 3
  • 6
  • 21