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)
Related
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:
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 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.
This question already has answers here:
Order of responses to MongoDB $in query? [duplicate]
(6 answers)
Closed 8 years ago.
Let's say I'm asking MongoDB for a query like this:
"someField" : {
"$in" : [9,3,7,1]
}
Will the returned objects be sorted by the order of the $in array?
IOW, when looking at the results, will I see all documents where {"someField" : 9} listed before the documents where {"someField":3 }, and then the 7's and then the 1's?
If not, any tips on how to get that?
Thanks!
Unfortunately, not only will the order of the $in array not affect the order of the results, but there also doesn't appear to be a built-in way to provide a custom sorting function to the sort function. You will mostly likely have to implement your own sorting function after retrieving the results. If you can tell us the language you're using to query MongoDB, we could probably help you with that part (if needed).
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