1

It's very easy to copy a javascript object in jQuery using jQuery.extend() function call. I am curious is there any way to do the same task in mongodb shell?

Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
  • 2
    Do any of these methods work in the Mongo shell? I would guess so ... http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-a-javascript-object – McGarnagle Jun 21 '12 at 06:56
  • No, MongoDB is not JQuery, there is another way. Let me find the link cos I have fogotten how to extend the console now – Sammaye Jun 21 '12 at 07:16

2 Answers2

1

Ok So if you see here:

http://www.mongodb.org/display/DOCS/Overview+-+The+MongoDB+Interactive+Shell#Overview-TheMongoDBInteractiveShell-.mongorc.js

You will see:

When the shell is launched, it checks the user's home directory for a javascript file named .mongorc.js.

This .mongorc.js allow you to define custom functions and what not for collection objects and db objects.

To go further here is an example stolen from an unsuspecting gist user: https://gist.github.com/1640154

Hope it helps,

Sammaye
  • 43,242
  • 7
  • 104
  • 146
  • I don't think this is the exact answer. I think you should explain how can it be done right in the answer. – cuneyttyler Jun 23 '15 at 14:19
  • @cuneytyvz well the OP accepted it. I do realise that extend is not the same as using a mongo.rc however, it seemed to answer the question as such it seems to be the answer. If you have a similar question you should ask it – Sammaye Jun 23 '15 at 14:30
  • @cuneytyvz to be fair as well this answer is from 3 years ago, it might be that it turned out that the OP was not clear abut what he was asking and the comments have since been deleted as being too chatty. I just don't remember I am afraid – Sammaye Jun 23 '15 at 14:32
  • whether op is accepted or not, it doesn't change the fact that it's not really an answer. – cuneyttyler Jun 23 '15 at 14:34
  • i see, i just wanted to note that :) – cuneyttyler Jun 23 '15 at 14:34
0

If I understand your question, you are seeking to add additional attributes to an existing document in your MongoDB collection. This can be done with the update method using the $set updater to add the new attributes, along with their values. The process is described in this MongoDB documentation page for $set.

Brian H
  • 254
  • 1
  • 5