11

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 the mongo shell i see the json object

> doc
{ "_id" : ObjectId("57325aaa48d9231b11e5cb81"), "username" : "anil", "email" : "mongodb@gmail.com" }

However when I type doc again, i dont see anything. its gone.. what am I doing wrong here ?

> doc
>

It might be something simple but I am not getting it. Can someone point out what I am doing wrong ?

styvane
  • 59,869
  • 19
  • 150
  • 156
user641887
  • 1,506
  • 3
  • 32
  • 50

3 Answers3

11

This because find returns a cursor and you can only consume all the value once. Here because you filter your document based on _id value, in the return cursor you only have one document which is consumed the first time you time. If you need to iterate your result many times, you will need to return an array that contains all the documents from the cursor using toArray, but in your case what you need is findOne

styvane
  • 59,869
  • 19
  • 150
  • 156
4

when you open a mongo shell it tries to find a mongod server and connects to test by default. when you assign some variable in mongo shell to some document in mongod, it actually fetches the document from the database, to get the same result again you need to connect to the database again (means you need to type var doc = db.users.find({"username":"anil"}); again). Unlike the scenario where you define var doc = 4 in shell and typing doc will return 4 every time.

If you want to stop transmission at the beginning and do some processing then you need to add null after it like

var doc = db.users.find({"username":"anil"});null; // do your work on doc

an ex.

enter image description here

amitava
  • 505
  • 1
  • 5
  • 10
  • so in this case when I do var doc = db.users.find({"username":"anil"});null; doc['username']="xyz" and then type in doc . would username value whic h was "anil" be replaced by "xyz" ? but when I try that, it does not work. – user641887 May 11 '16 at 22:11
  • please see my added comments above. – amitava May 11 '16 at 22:49
  • i guess i misinterpreted your requirement. if you want to update the original table in the mongod with the updated document you need to add just one more step. I updated my earlier append. please feel free to let me know if this is not your requirement. – amitava May 12 '16 at 08:09
  • also please follow the exact steps i mentioned. it seems you missed the step---- while (doc.hasNext()) doc1 = doc.next();----- then edit the doc1 NOT the doc. you will not get anything if you try to edit doc like the way you showed. – amitava May 12 '16 at 09:31
  • user641887, please switch the accepted answer from this unintelligible one to the correct one by @styvane below – foobarbecue Jul 13 '19 at 23:50
0

One other option could be to use the next() function of the find() cursor. This will give the value of the first record in to the variable and it will persist. Then any required operation can be done on the variable.

Apurva Kunkulol
  • 445
  • 4
  • 17