I do next:
db = client["database"]
home = db['home'].find() # get collection.
db['home'].remove({}) # remove doc from home
for i in home:
self.db['home'].insert(i)
But the collection is empty.
MongoDB – copyTo() Method In MongoDB, copyTo() method is used to copies all the documents from one collection(Source collection) to another collection(Target collection) using server-side JavaScript and if that other collection(Target collection) is not present then MongoDB creates a new collection with that name.
To delete all documents in a collection, pass an empty document ( {} ). Optional. To limit the deletion to just one document, set to true . Omit to use the default value of false and delete all documents matching the deletion criteria.
To clone a document, hover over the desired document and click the Clone button. When you click the Clone button, Compass opens the document insertion dialog with the same schema and values as the cloned document. You can edit any of these fields and values before you insert the new document.
The db. copyDatabase method us used to copy a database either from one mongod instance to the current mongod instance or within the current mongod. Name of the source database.
In Studio 3T, you can reliably (and easily) copy MongoDB documents and paste them across databases and collections. Download our MongoDB GUI, and let’s get started. 1. Open your source MongoDB collection in Studio 3T and select the documents that you would like to copy – either by selecting the documents themselves or any of their fields.
Duplicate collection tool of NoSQL Manager for MongoDB Duplicate Collection is a professional feature of NoSQL Manager for MongoDB Pro. It allows to duplicate collection very quickly within the same database. Right-click on collection1 collection in DB Explorer and select Duplicate 'collection1' Collection... item in the popup menu.
Download the latest Studio 3T version and let’s roll. If you’re looking to copy entire MongoDB databases across servers, here’s that article. Select the source collection you want to copy and right-click it in the Connection Tree. In our case, this is collection test.people on localhost. Choose Copy Collection.
Mongodb returns a cursor which is a pointer to documents in resultset upon execution of find operation and by default it will fetch all documents in a collection unless limit is applied to find operation. Is it better to save multiple documents or less documents with large objects in mongodb? There’s no single best practice.
The problem with your code example is that find()
returns a database cursor to the collection, not all documents in the collection. So when you remove
all documents from the home
collection, the cursor will also point to an empty collection.
In order to copy a collection to another collection in the same server, you can utilise MongoDB Aggregation operator $match and $out
pipeline = [ {"$match": {}},
{"$out": "destination_collection"},
]
db.source_collection.aggregate(pipeline)
Using your example code, now you can do
source = db["source_collection"]
destination = db["destination_collection"]
# Remove all documents, or make modifications.
source.remove({})
# Restore documents from the source collection.
for doc in destination:
source.insert(doc)
# or instead you can just use the same aggregation method above but reverse the collection name.
Note : db.collection.copyTo() has been deprecated since MongoDB v3.0.
If you would like to copy to another MongoDB server, you can utilise db.cloneCollection(). In PyMongo it would be a command such below:
db.command("cloneCollection", **{'collection': "databaseName.source_collection", 'from': "another_host:another_port"})
Depending on your overall goal, you may find MongoDB BackUp methods useful.
This could be the easiest way to do that, I personally prefer it, so you can add as many filters as you like:
from pymongo import MongoClient
def CopyFromColl1ToColl2(database1,collection1,database2,collection2):
db1 = MongoClient('mongodb://127.0.0.1:27017')[database1][collection1]
db2 = MongoClient('mongodb://127.0.0.1:27017')[database2][collection2]
#here you can put the filters you like.
for a in db1.find():
try:
db2.insert(a)
print(a)
except:
print('did not copy')
# You can choose the database name and the collection name
CopyFromColl1ToColl2('database1','collection1','database2','collection2')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With