1

I want to save the result of the find() command to a new collection (based on an answer to a similiar question Saving the result of a MongoDB query).

> db.collection2.insert(db.collection1.find({"person.name": "Carl"}))

then I wanted to see whether this was successfull

> db.righthand.find()
[Object]

I am not sure why it outputted [Object] because I assumed it would insert the result of db.collection1.find().

xava
  • 293
  • 1
  • 2
  • 16

1 Answers1

1

You are getting [Object] in your console because you inserted a cursor returned by the find() method.

What you really need is to use the toArray() method on the cursor as it returns an array that contains all the documents from it. The method iterates completely the cursor, loading all the documents into RAM and exhausting the cursor.

So your insert operation would look like

> db.collection2.insert(db.collection1.find({"person.name": "Carl"}).toArray())

And query the collection as

> db.collection2.find().pretty()
chridam
  • 100,957
  • 23
  • 236
  • 235