Remove element of internal field array MongoDb [duplicate] - mongodb

This question already has answers here:
MongoDB, remove object from array
(7 answers)
Closed 4 years ago.
I have the following situation:
{ code: 0, array:[{ _id: 1, ....}, { _id: 2, ....}, { _id: 18, ....}]}
I need to remove the element inside 'array' field matching the '_id' field.
How can I do that?
Thank you.

Use $update & $pull:
db.yourCollection.update(
{ }, // <-- your selection criteria
{ $pull: { array: { _id: 2 } } // <-- what you want to remove from which field
)

Related

How to update nested object property in MongoDB [duplicate]

This question already has answers here:
MongoDB - Update an object in nested Array
(4 answers)
Closed 1 year ago.
I have a data on MongoDB database which structure is
{
_id: 12478565,
products: { name: 'Mackbook Pro', price: 150000, color: 'silver' }
}
I want to update the products price and I write like this, but it gives an error
productsCollection.updateOne({_id: '12478565'}, {$set: {products.price: 190000} })
I want to know how can I modify the data in nested object?
put product.price in qoute
db.collection.update({
_id: "12478565"
},
{
$set: {
"products.price": 190000
}
})
https://mongoplayground.net/p/QxAK0LClF-8

How can I query an array in Mongo which includes some of a list of items? [duplicate]

This question already has answers here:
Find document with array that contains a specific value
(13 answers)
Closed 2 years ago.
For example, if these are the documents:
{
"_id": 1,
"test_array": [1,2,3,5]
},
{
"_id": 2,
"test_array": [4,5]
},
{
"_id": 1,
"test_array": [1,2,3]
}
And I want to get all the documents which "test_array" includes 4 or 5 (aka doc 1 and 2).
Is there a simple query for it?
$in
the $in operator selects the documents where the value of a field equals any value in the specified array
so just try
db.collection.find({
test_array: {
$in: [
4,
5
]
}
})
mongoplayground

MongoDB update nested array of object by index [duplicate]

This question already has answers here:
Update field in exact element array in MongoDB
(5 answers)
Closed 5 years ago.
Let's suppose to there is db like below...
{ _id: 1234,
key: 'Contacts',
value: [
{ name: 'McDonald', phone: '1111'},
{ name: 'KFC', phone: '2222'}
]
}
And I want to change KFC's phone number to '3333'.
What I did is
DB.findOne({ key: 'Contacts' }, function(err, db){
db.value[1]['phone'] = '3333'
db.save(function(err, result){
// done
})
}
)
But it didn't update the database. What am I wrong?
There's no specific _id in elements of array for some reason.
Only the way to find specific element is index.
Use the positional operator $
more info : https://docs.mongodb.com/manual/reference/operator/update/positional/#up.S
DB.update({key: "Contacts", "value.name": "KFC" },
{ $set: { "value.$.phone" : 666 } },function(err,doc){
});

mongoDB: check if value exists in specific subdocument array [duplicate]

This question already has answers here:
Retrieve only the queried element in an object array in MongoDB collection
(18 answers)
Closed 5 years ago.
I have the following schema:
{
_id: objectID('593f8c591aa95154cfebe612'),
name: 'test'
businesses: [
{
_id: objectID('5967bd5f1aa9515fd9cdc87f'),
likes: [objectID('595796811aa9514c862033a1'), objectID('593f8c591ba95154cfebe790')]
}
{
_id: objectID('59579ff91aa9514f600cbba6'),
likes: [objectID('693f8c554aa95154cfebe146')]
}
]
}
How can I check if objectID('593f8c591ba95154cfebe790') exists in the array business.likes where businesses._id = objectID('59579ff91aa9514f600cbba6') ?
In the above example it should return false.
You can use $elemMatch to find by multiple field in same sub document
db.collectionName.find({
businesses: {
$elemMatch: {
_id: ObjectId("5967bd5f1aa9515fd9cdc87f"),
likes: ObjectId("593f8c591ba95154cfebe790")
}
}
});

Increment all element in nested array [duplicate]

This question already has answers here:
How to Update Multiple Array Elements in mongodb
(16 answers)
Closed 8 years ago.
I have a document like that :
{
_id: ".....",
messages: [
{
....
votes: 2
},
{
....
votes: 2
}
]
}
I would like to increment ALL votes field in the array in the same request.
How to do that ? The $ operator select only the first element.
Thank you !
You might have to do this with some JS loops:
db.collection.find( { "_id": SOME_ID } ).forEach( function( doc ) {
for ( i in doc.messages ){
doc.messages[ i ].votes += 1;
}
db.collection.save( doc );
} );
What I'm doing here is iterating over each document in the cursor and iterating over it's messages property incrementing each element's votes attribute by one.