Reputation: 85316
I want to clone an existing collection, including data and indexes, to a new collection with another name within the same database, using mongodb JSON interface (not the command-line interface).
I've tried:
cloneCollection
- didn't work. is for cloning across databases.
aggregate
with an $out
operator - that copies just the data but not the indexes.
The aggregate command I've tried:
{"aggregate":"orig_coll", "pipeline":[{"$out":"orig_clone"}]}
Upvotes: 2
Views: 1638
Reputation: 5562
There is no way to do this in one JSON query.
So, two solutions here :
mongodump
/mongorestore
as proposed in What's the fastest way to copy a collection within the same database?What you need to understand is that, the JSON interface as you told it is not a database interface but a database JavaScript query language. So you can pass query to it not command. In fact, it's not an interface just a query DSL. The interface is the mongo shell or any of the mongo drivers (java, perl, ...) or any of the mongo admin tools ...
Upvotes: 1