Deleting a document that matches a query in pymodm - mongodb

Please help, been stuck for hours, how do I delete a document that matches a query directly in pymodm.
For example:
user = User.objects.raw({'name':'Moses'})
How do I delete this user from my database collection?

you can use the delete() method to delete document from the MongoDB, try the below code this worked for me fine.
user = User.objects.raw({'name':'Moses'})
user.delete()

Related

Flutter Firebase delete a document of a subcollection using the document ID

Apparently, I was able to retrieve a subcollection data from firebase by using the code below
FirebaseFirestore.instance.collectionGroup('announcementlist').where('id', isEqualTo: '${docID}');
However, I am now trying to delete a subcollection by using this collectionGroup method and it is not working. I want all admin user to be able to delete the subcollections.
So what I found online is something similar to this line of code.
// define document location (Collection Name > Document Name > Collection Name >)
var docRef = Firebase.firestore().collection("Rooms").doc("bsYNIwEkjP237Ela6fUp").collection("Messages");
// delete the document
docRef.doc("lKjNIwEkjP537Ela6fhJ").delete();
which I got from codegrepper but as far as I know, the doc("bsYNIwEkjP237Ela6fUp") part requires the id of the user which is not the method I want because all admin users should be able to delete the data and it uses collection instead of collectiongroup
Is there any solution for this ? Please help.
FirebaseFirestore.instance.collection("Rooms").doc(snapshot.requireData.docs[i].id).collection("Messages").doc(snapshot.requireData.docs[i].id).delete();

Getting the last inserted document Id

I have tried to get the last saved documentId from the mongo database. But whatever I try I don't get anything back.
Can someone please hint me how to solve this in C# i have tried doing the sorts and take the last document but i don't get anything back.
MongoDB does not have such an operation.
If you want to know the id of inserted document, generate the id on the client side before inserting.

Why does my update $set code remove my entire document?

Help!
I don't what am doing wrong, when I try to update an existing field using the $set method the entire document gets removed.
Can you kindly point out what I am doing wrong in my code:
recipientsDetails.update({_id: "GCYmFqZbaaYD7DvMZ"}, {$set: {paymentStatus: "Approved"}});
Thanks for your help!
The code is correct. It's likely that your publish function for recipientsDetails contains recipientsDetails.find({paymentStatus: "Not Approved"}). Naturally, once you update the document, the document will no longer satisfy that filtering query and the document vanish from the client.
Your code is correct.Check you mongoDB using Robomongo tool.connect your local project with robomongo and update a document then check whether it is updated or not? If the record updated there is an issue with the publish or subscriptions

Remove all fields except one from mongodb document

This Meteor server code needs to remove all fields except "fName" from a document found by a field and if the document does not exist then create it.
Is there a way to do that at one go? thx
myCol.update({fName: someName}, {fName: someName}); // works if doc exists, fails if no doc.
myCol.upsert({fName: someName}, {fName: someName}); // failed if doc exists, works if it exists
You can use fName :{$exists:true} in your query part.
This will update document only if fName in present.

Cannot remove document using obejct id of MongoDB

I need to remove one document from my database but unable to do it using MongoDB. Here is my query:
db.kf_feedback_pro_serv.remove({_id: ObjectId("575a8a0ae4017ae81400002e")})
Using the above query I cannot delete the document.
There is no method as remove. Use deleteOne instead. Also, you can just provide the ID without the ObjectID(...)
Try this:
db.kf_feedback_pro_serv.deleteOne({_id: "575a8a0ae4017ae81400002e"})