This question already has answers here:
Mongodb Query To select records having a given key
(3 answers)
Closed 5 years ago.
I have stored the data in mongodb database.My each entry looks like below:
{u'_id': ObjectId('5926858fd51b6b4a399a800a'), u'james,2017-05-01': [u'2017-05-01', u'Cell', u'1', u'UT', u'Repo', u'1', u'Add extra information']}
As you can see my key is combination of username and date,i.e.username,data.
And the value is the list.
I want to remove the entry in the database using key in my case it is username,data.
I am new to MongoDB. Can someone help me?
Basic syntax of remove() method is as follows −
db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
Consider the test collection has the following data.
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"First"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"Second"}
Following example will remove all the documents whose title is 'First'.
db.test.remove({'title':'First'})
To Check :
db.test.find()
Related
This question already has answers here:
MongoDB - Update or Insert object in array
(10 answers)
Closed 2 years ago.
I have the following document:
{
"_id":ObjectId("idhere"),
"locations":[
{
"latitude":3453.4353,
"longitude":31.123
},
{
"latitude":1243.134,
"longitude":992.321
}
]
}
The question is:
How can I update this document locations' without overriding the entire list,just by appending a new location to the existing ones? I know that to update a document I have to use this query:
db.collection.update({ _id : ....}, {$set : { locations : ... }}) but here is the problem -> in this way I should get the existing locations before, add the new location to this array and execute the query with the updated array which would take a lot of time & resources if the locations array grows.
Any suggestions? (P.S.: I'm currently using the MongoDB in a spring-boot project and I'm using MongoRepository interface to communicate with the DB.)
I've found the solution for this question in the mongodb docs here at the "Examples" section.
A more detailed picture here:
The saving solution is to use $push instead of $set.
This question already has answers here:
Update field in exact element array in MongoDB
(5 answers)
MongoDB: How to update multiple documents with a single command?
(13 answers)
Update MongoDB field using value of another field
(12 answers)
Closed 5 years ago.
I have a collection in MongoDB that contains a list of objects. Each object has an array of "updaters" which are objects with a field that I want to update. One of the updaters in that array needs a description updated. How can i update just the descrption of the the updater object, that I know "updaterId" of.
Update:
It appears to me that setting the flag multi: true will do the trick in Mongo 2.2+.
db.configurations.update(
{"updaters.updaterId": "90391154-67BB-452E-A1A7-A07A98B94F86"},
{$set: {"updaters.$.description": "This tool will prevent users from Unloading Linked Revit files for \"all users\" which causes such Linked File to be unloaded by default when opening project."}},
{multi: true}
)
You can update the description updaterId like this:
db.collection.update( {"updaters.updaterId" : updaterId } ,
{$set : {"updaters.$.description" : "My description"}} ,
false ,
true);
This question already has answers here:
How do I rename fields when performing search/projection in MongoDB?
(4 answers)
Closed 6 years ago.
My result is given below;
db.userdetail.find({}, {UserName: 1, _id:1}).pretty();
Result:
{
"_id" : ObjectId("572981ddbe0da6156de04167"),
"UserName" : "anil"
}
Here how do I change UserName to name and _id to value, is it possible?
You could use mongodb's aggregation framework, although I would not recommend it for big queries (you might hit memory limits) or very common queries (consumes more resources)
This question already has answers here:
MongoDB Aggregation - match if value in array
(9 answers)
Closed 6 years ago.
Lets say I have a collection called Messages and a message can have multiple parents which can be found by checking if the message_id is in the parent_id array, where the parent_id is an array.
{
message_id : 22,
parent_id: [22,11],
}
How would I create a mongo query to find that?
Right now I have the following but it fails:
db.messages.find({this.message_id: { $in: this.parent_id}})
As you probably know mongo db does not support joins, I recommend you to do this at application level, using mongo shell you could run something like this.
//Get the message in which you are interested to get their parents
var child = db.messages.findOne({message_id: 22});
//Go with the array to search the parents.
db.messages.find({message_id : { $in : child.parent_id }});
I think you need to do in two steps, unless somebody knows a more elegant solution.
This question already has answers here:
MongoDB : aggregation framework : $match between fields
(2 answers)
Closed 8 years ago.
I want to compare one key value with another key value in mongodb. can any one help me with this query.
example:
{
"_id":"xxxxx",
"game":"xxx",
"score":100,
"hi-score":200
}
i want to check if score is greater than hi-score
Try the below query using http://docs.mongodb.org/manual/reference/operator/query/where/
db.collection.find( { $where: "this.score > this.hi-score" } );
Also, please read the warning section provided in the link.
PS: I have not tried this