-
-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make the 'insert' and 'update' methods not modify the past Map #236
Comments
Yes, |
this caused me a problem, because I do an insertion and then an update of the same map in different collections, the first collection being a history of changes, and the second collection is the main one, but when I go to save in the second collection the error is due to that the first insert modifies the Map by putting an _id |
maybe a solution for this would be the insertOne method to clone the Map so that it doesn't modify the original |
Let's say that I have a map (
var documentKey = ObjectId();
myMap['_id'] = documentKey;
await history.insertOne(myMap); This way I have the map ready to be inserted in another collection ( await actual.insertOne(myMap); If you need to update a document on actual.replaceOne( where.id(myMap['_id']), myMap); or actual.replaceOne( where.id(documentKey), myMap);
So, filling the |
Sorry, if I cannot help you anymore, but it is still unclear to me what you would like to do. |
Let us assume this situation: I have a Map so it's also stored in the 'coupons' collection :var data = {"id":123456,"name":"Isaque", "age":36} I want to be included in the 'usedCoupons' collection await db.collection('usedCoupons').insertOne(data); and then I want to update the 'coupons' collectionawait db.collection( 'coupons').replaceOne({'id':123456}, data); This code will fail as the insertOne method added an '_id' to Map dataA possible solution would be the insertOne method to clone the variable data like thisfinal Map<String, dynamic> documentClone = {...document}; |
I do not know if it was a typo or not but beware that "id" and "_id" are two different things for mongoDb. var data = {"_id":123456,"name":"Isaque", "age":36} If the map have been inserted in a collection it should contain an "_id" field. await db.collection('usedCoupons').insertOne(data); Now we insert the data map inside the "usedCoupons" collection with the same data['counter']++;
await db.collection( 'coupons').replaceOne({'_id':123456}, data); No we can update the "coupons" collection using the unique identifier Hope this can help. |
I don't think you understand, I don't use the "_id" field, because I have my own method of generating unique "id", so I use an "id" field instead of "_id", my solution was to make a wrapper on top of the driver so that the insertion into the database is done without changing the Map passed via parameter, this was done easily through the spreed operator cloning the Map, by design I think the driver should not modify the instance of Map passed by parameter, it should clone the Map and modify the clone and not the original instance. my problem is not mongo putting the "_id" field, the problem is the driver modifying the Map instance passed via parameter |
What I do not understand is why you do not use the |
make the 'insertOne' and 'replaceOne' ... methods not modify the past Map,
to prevent the error, "MongoError: The (immutable) field '_id' was found to have been altered"
I verified that the insertOne method was putting the "_id" in the Map which was causing me problem
The text was updated successfully, but these errors were encountered: