This question already has answers here:
MongoDB: query for a field
(1 answer)
MongoDB - how to query for a nested item inside a collection?
(3 answers)
Closed 8 years ago.
here is an example structure of one object in my MongoDB:
{
"itemClass": 4,
"containerSlots": 0,
"itemBind": 1,
"weaponInfo": {
"dps": "0.0",
"damage": {
"maxDamage": 0,
"minDamage": 0
},
"weaponSpeed": "0.5"
}
}
I need to find maxDamage which is 1
here is my query:
db.items.find({"maxDamage" : "1"}).limit(3)
but it returns nothing
p.s. there are a lot of objects with attribute maxDamage is 1
You have to use dot notation like this:
db.items.find({"weaponInfo.damage.maxDamage": 1})
Related
This question already has answers here:
How do I perform the SQL Join equivalent in MongoDB?
(19 answers)
MongoDB Aggregate lookup in Go (mgo.v2)
(1 answer)
Closed 4 years ago.
I have two collections.
I am using MongoDB version 3.4.
Collection 1 called Results
Collection 2 called Code_Value
Here i have to update automatically the code in the result using the Code_value Collections
The results is like:
{
_id: ObjectId(xxxxxxx)
name:"Mike"
job:"3"
salary:"4"
}
{
_id: ObjectId(xxxxxxx)
name:"Joe"
job:"1"
salary:"2"
}
the Code_value is like:
{
"Job":[
{"code":"1" , "value":"IT"}
{"code":"2" , "value":"Doctor"}
{"code":"3" , "value":"Developer"}
]
"Salary":[
{"code":"1" , "value":"900000"}
{"code":"2" , "value":"100000"}
{"code":"3" , "value":"200000"}
{"code":"4" , "value":"300000"}
]
}
I'd like to have a mapping code value on the results as the following:
{
_id: ObjectId(xxxxxxx)
name:"Mike"
job:"Developer"
salary:"300000"
}
{
_id: ObjectId(xxxxxxx)
name:"Joe"
job:"IT"
salary:"100000"
}
here is the query made as function
results.find().foreach(cursor){
v=Code_value.find(cursor,code)
if(v){
results.update(job,v.value)
}
This question already has answers here:
MongoDB: How do I update a single subelement in an array, referenced by the index within the array?
(10 answers)
How do I update Array Elements matching criteria in a MongoDB document?
(2 answers)
Closed 4 years ago.
I have the following document:
{
_id: objectId("5aa7abb3c7dc133b0007e6e"),
name: "first course",
targets: Array
0: Object
id: "11111_rrrr",
value: "test"
1: Object
id: "22222_rrrr",
value: "hi"
}
so, I want to change the value of the array[i].
my solution is :
$update = array(
'targets.$[].value' => "New Value"
);
$result = Course::where("_id", $course_id)->update($update);
it changes all the value of the array, how can I solve my problem??
Try this
db.getCollection('TEST').update({"targets.id" : $course_id},{$set:{"targets.$.value" : "New Value"}})
This question already has answers here:
Update field in exact element array in MongoDB
(5 answers)
Closed 6 years ago.
user1 in database
{
counts: [{code:"aaa", num:100}, {code:"bbb", num:200}]
}
counts is an Array, it has two elements code and num.
How to let num increase 1 if code = "aaa"?
Meteor.users.update(user1._id, {$inc: {???}}); // How to write this?
Thanks
Gotta positionally match. Example:
Players.update({
_id: Meteor.userId(),
'games._id': Session.get('gameId')
}, {
$inc: { 'games.$.score': 1 }
});
This question already has answers here:
Mongodb Query To select records having a given key
(3 answers)
Closed 7 years ago.
I have documents stored in mongo database following this schema:
{
map:{
key1:value,
banana:value2
....
}
}
How can I query objects based on keys in this map ?
e.g I want to get all the documents which map contains key that equals banana.
Maps are accessed the same way as normal nested values.
This means that you can use the $exists operator to check if the key exists.
db.collection.find( { "map.banana" : { $exists : true } } );
This question already has answers here:
How to Update Multiple Array Elements in mongodb
(16 answers)
Closed 4 years ago.
If I have a collection mycoll of documents like this :
{
vals : [10, 11]
}
where the length of the vals array is NOT fixed.
is there a way to $inc ALL the elements in the vals array with one command ?
I've tried
db.mycoll.update({}, { $inc : { vals : 5 }}, { multi : true })
// error cannot $inc non-number
db.mycoll.update({}, { $inc : { vals.$ : 5 }}, { multi : true })
// error must specify a field
and the goal is to get
{
vals : [15, 16]
}
without having to fetch each document, copy and update the array, then save the updated array back to the document...
my thanks in advance for any ideas !
whoops, there is already a JIRA open for this here
https://jira.mongodb.org/browse/SERVER-1243
You can include multiple fields in your $inc, so while you need to explicitly reference each element by its index, you can do this as:
db.mycoll.update({}, {$inc: {'vals.0': 5, 'vals.1': 5}})