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
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:
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()
This question already has answers here:
MongoDb query condition on comparing 2 fields
(4 answers)
Closed 6 years ago.
Is it possible in mongoDB to query documents where a field's value is greather than another one of the same document ?
I want to query each document where a > b for documents as:
{
a: Number,
b: Number,
}
Is it possible to reference a field's value in query to another one ?
Yes it is possible. Consider that you are trying to get the documents where a is greater than b. The code below is doing that.
db.collectionName.find({$where: "this.a > this.b"})
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 query condition on comparing 2 fields
(4 answers)
Closed 8 years ago.
Is it possible to find all documents in a collection where documents with field (_id) values exist in another field (ex. parentID). Taking in consideration that both fields exist in the documents of the same collection? Thanks
Categories.find({'_id': 'parentID'})
{
_id: 11,
parentID: 1
}
I am using MongoDB 2.6.7
Yes, this is easy to do with the $where operator.
db.Categories.find({'$where':"this._id === this.parent"})
This gives you more flexibility than your regular find syntax but be warned that MongoDB needs to evaluate the Javascript so this is slower than a regular query.