I have a database like this:
[
{
"universe":"comics",
"saga":[
{
"name":"x-men",
"characters":[
{
"character":"wolverine",
"picture":"618035022351.png"
},
{
"character":"cyclops",
"picture":"618035022352.png"
}
]
}
]
},
{
"universe":"dc",
"saga":[
{
"name":"spiderman",
"characters":[
{
"character":"venom",
"picture":"618035022353.png"
}
]
}
]
}
]
and with this code I manage to update one of the objects in my array. specifically the object where character: wolverine
db.mydb.findOneAndUpdate({
"universe": "comics",
"saga.name": "x-men",
"saga.characters.character": "wolverine"
}, {
$set: {
"saga.$[].characters.$[].character": "lobezno",
"saga.$[].characters.$[].picture": "618035022354.png",
}
}, {
new: false
}
)
it returns all my document, I need ONLY the document matched
I would like to return the object that I have updated without having to make more queries to the database.
Note
I have been told that my code does not work well as it should, apparently my query to update this bad, I would like to know how to fix it and get the object that matches these search criteria.
In other words how can I get this output:
{
"character":"wolverine",
"picture":"618035022351.png"
}
in a single query using filters
{
"universe": "comics",
"saga.name": "x-men",
"saga.characters.character": "wolverine"
}
My MongoDB knowledge prevents me from correcting this.
Use the shell method findAndModify to suit your needs.
But you cannot use the positional character $ more than once while projecting in MongoDb, so you may have to keep track of it yourself at client-side.
Use arrayFilters to update deeply nested sub-document, instead of positional all operator $[].
Below is a working query -
var query = {
universe: 'comics'
};
var update = {
$set: {
'saga.$[outer].characters.$[inner].character': 'lobezno',
'saga.$[outer].characters.$[inner].picture': '618035022354.png',
}
};
var fields = {
'saga.characters': 1
};
var updateFilter = {
arrayFilters: [
{
'outer.name': 'x-men'
},
{
'inner.character': 'wolverine'
}
]
};
db.collection.findAndModify({
query,
update,
fields,
arrayFilters: updateFilter.arrayFilters
new: true
});
If I understand your question correctly, your updating is working as expected and your issue is that it returns the whole document and you don't want to query the database to just to return these two fields.
Why don't you just extract the fields from the document returned from your update? You are not going to the database when doing that.
var extractElementFromResult = null;
if(result != null) {
extractElementFromResult = result.saga
.filter(item => item.name == "x-men")[0]
.characters
.filter(item => item.character == "wolverine")[0];
}
I have Collection named Account
This is this collection:
userID: "1111111111111111111"
player: {
something:{
something2: {
smth: 99
},
something3: {
smth: 88
}
}
}
I want to add something4 {smth: 77}" into something from other collection or json file.
I tried with Collection.findOneAndUpdate or Collection.insertOne but nothing working.
You are going the right direction with collection.findOneAndUpdate()
you need to do:
collection.findOneAndUpdate({usrID:"1111111111111111111"}, [{
$set:{something4:{smth: 77}}
}]);
See the update operators page https://docs.mongodb.com/manual/reference/operator/update/
I have Mongoose schema like this:
{
......
project: [
{
Name: String,
Criteria:[
{
criteriaName:String,
}
]
}
]
......
}
And I want to remove one of the objects of criteria array which is in the array of project based on the object id
I tried the code following
criteria.findOneAndUpdate({
"_id": uid,
},{ $pull: { "project.Criteria": { _id: cid } } }, (err) => {
......
}
However this cannot work, it said "Cannot use the part (Criteria) of (project.Criteria) to traverse the element"
Do you need to do it in one query to the database? If not, the following solution may work for you:
criteria.findOne({ _id: uid })
.then((obj) => {
// Filter out the criteria you wanted to remove
obj.project.Criteria = obj.project.Criteria.filter(c => c._id !== cid);
// Save the updated object to the database
return obj.save();
})
.then((updatedObj) => {
// This is the updated object
})
.catch((err) => {
// Handle error
});
Sorry if the .then/.catch is confusing. I can rewrite with callbacks if necessary, but I think this looks a lot cleaner. Hope this helps!
Hi all I am very new in mongodb and I have some problems when I try to do some find in a collections I hope so someone can help me.
Imagine I have the following collection:
db.fechasCambio.insert(
{
columns:[
'Divisa',
'05/10/2015',
'02/10/2015',
'01/10/2015',
'30/09/2015',
'29/09/2015',
'28/09/2015',
'25/09/2015',
'24/09/2015',
],
data:[
{
'Divisa':'USD',
'05/10/2015':'11236',
'02/10/2015':'1116',
'01/10/2015':'11153',
'30/09/2015':'11203',
'29/09/2015':'11204',
'28/09/2015':'1117',
'25/09/2015':'11151',
'24/09/2015':'11241'
},
{
'Divisa':'BGD',
'05/10/2015':'19558',
'02/10/2015':'19558',
'01/10/2015':'19558',
'30/09/2015':'19558',
'29/09/2015':'19558',
'28/09/2015':'19558',
'25/09/2015':'19558',
'24/09/2015':'19558'
},
{
'Divisa':'CYP',
'05/10/2015':'0',
'02/10/2015':'0',
'01/10/2015':'0',
'30/09/2015':'0',
'29/09/2015':'0',
'28/09/2015':'0',
'25/09/2015':'0',
'24/09/2015':'0'
}
]
}
) c
Now I want do some find inside the collections for example:
A-I want to bring me all currencies search by date range 05/10/2015 to 01/10/2015 then I need that return something like this:
{
columns:[
'Divisa',
'05/10/2015',
'02/10/2015',
'01/10/2015',
],
data:[
{
'Divisa':'USD',
'05/10/2015':'11236',
'02/10/2015':'1116',
'01/10/2015':'11153',
},
{
'Divisa':'BGD',
'05/10/2015':'19558',
'02/10/2015':'19558',
'01/10/2015':'19558',
},
{
'Divisa':'CYP',
'05/10/2015':'0',
'02/10/2015':'0',
'01/10/2015':'0',
}
]
}
I want to know what is the structure of the find that I need to use for have a result similar.
B-Also I need to know how do a find that return all the information related with a currency, for example I want searh all releated with the currency 'USD', I need return something similar to:
{
columns:[
'Divisa',
'05/10/2015',
'02/10/2015',
'01/10/2015',
'30/09/2015',
'29/09/2015',
'28/09/2015',
'25/09/2015',
'24/09/2015',
],
data:[
{
'Divisa':'USD',
'05/10/2015':'11236',
'02/10/2015':'1116',
'01/10/2015':'11153',
'30/09/2015':'11203',
'29/09/2015':'11204',
'28/09/2015':'1117',
'25/09/2015':'11151',
'24/09/2015':'11241'
}
]
}
I want to know what is the structure of the find that I need to use for have a result similar.
C-And finally I need to know how do a find that return all the information related with a value, for example I want search all the information with value of 0 between 12000, I need return something similar to:
{
columns:[
'Divisa',
'05/10/2015',
'02/10/2015',
'01/10/2015',
'30/09/2015',
'29/09/2015',
'28/09/2015',
'25/09/2015',
'24/09/2015',
],
data:[
{
'Divisa':'USD',
'05/10/2015':'11236',
'02/10/2015':'1116',
'01/10/2015':'11153',
'30/09/2015':'11203',
'29/09/2015':'11204',
'28/09/2015':'1117',
'25/09/2015':'11151',
'24/09/2015':'11241'
},
{
'Divisa':'CYP',
'05/10/2015':'0',
'02/10/2015':'0',
'01/10/2015':'0',
'30/09/2015':'0',
'29/09/2015':'0',
'28/09/2015':'0',
'25/09/2015':'0',
'24/09/2015':'0'
}
]
}
I want to know what is the structure of the find that I need to use for have a result similar.
I am trying to update an Array object based on a condition. Following is my scenario :-
I want to update status from current to archive.
I have tried many things for hours but still no luck. Like this :-
db.user.update({
'injury._id': ObjectId("5374cb4d1e0386c02800006a"),
'injury.injurydata.locationaddressinjury': {
$elemMatch: {
'status': 'current'
}
}
}, {
$set: {
'injury.injurydata.locationaddressinjury.status': 'archive'
}
})
The picture made it hard to read the structure of your data. But I guess the update you are looking for would be something like this:
db.user.update({
'injury._id': ObjectId("5374cb4d1e0386c02800006a"),
'injury.injurydata.locationaddressinjury': {
$elemMatch: {
'status': 'current'
}
}
}, {
$set: {
'injury.injurydata.locationaddressinjury.$.status': 'archive'
}
});
The $ would refer to the element you found. While if you are looking for a way to update all element at one time. I'm afraid $elemMatch would just match the first element that satisfies your condition.