How to update each document with one query in mongodb [duplicate] - mongodb

This question already has an answer here:
How to update each value with one query in mongodb
(1 answer)
Closed 3 years ago.
I have a data as below.
// first document
{
"id": 1,
"exist": true
}
// second document
{
"id": 2,
"exist": false
}
// third document
{
"id": 3,
"exist": false
}
When I findOneAndUpdate({_id:2}),{exist:true}), I hope that exist of 'id:1' is changed to false automatically in one query using aggregate or etc.
could you recommend some idea for it? Thank you so much for reading my question.

I don't really understand what do you want to update in your collection but to update many document use updateMany :
db.collection.updateMany(
{ violations: { $gt: 1 } }, // the filter to select the wanted document
{ $set: { "exist" : true } } // the change to apply to the field
);

Related

Mongo if inside $match and $or [duplicate]

This question already has answers here:
mongo in() clause sort by most matches
(2 answers)
Closed 9 months ago.
I've been stuck with this for a quite while.
I want to filter specials.name field based on two inputs from my form - select and input field. The problem is, whatever I tried, it always chooses on of it.
Is there any way to put if, else block inside $or to make it work?
This is the last thing that I tried, and now it works always on searchInput, which is input field, it does not react on changes in select menu.
{
$match: {
$or: [
{ 'specials.name':{$in :[searchSpecials, searchInput]}},
]
}
}
const searchSpecials = req.body.searchSpecials
const searchInput = req.body.search
If the two inputs are strings, this works as expected:
db.collection.aggregate([
{
$match: {"specials.name": {$in: ["Malinda", "Joseph"]}}
}
])
and returns:
[
{
"_id": ObjectId("5a934e000102030405000000"),
"specials": {
"name": "Malinda"
}
},
{
"_id": ObjectId("5a934e000102030405000002"),
"specials": {
"name": "Joseph"
}
}
]
As you can see here, on the playground.
No need for the $or, since the $in will return all documents that have specials.name which is included in the list of options.

mongodb query object inside an array [duplicate]

This question already has answers here:
Query for a field in an object in array with Mongo?
(2 answers)
Closed 3 years ago.
So I have a collection called Transaction that is structured like the following
{
"_id": "1",
"type": "purchase",
"amount": 11.8,
"relatedObjects": [
{
"eventId": "123131313131322"
}
],
"paymentMethod" : "method1"
}
I want to query according to paymentMethod and eventId. So I was doing the following but it is not working.
db.Transaction.find({ "paymentMethod": "method1"});
and this is how I go all the transactions for "method1" but I am not sure on how should I query for eventId inside of related objects. I tried something like
db.Transaction.find({"paymentMethod": "method1", "relatedObjects[0].eventId" : "123131313131322" })
I didnt seem like it was going to work in my head but I have no clue on how to query an object inside an array.
Damn I had to read for third time the docs, sorry,
It was something like
{
"paymentMethod": "method1",
"relatedObjects" : {
$elemMatch : {
"eventId": "13213213212131321321"
}
}
}

Mongo Query in Nested Json Array [duplicate]

This question already has answers here:
Retrieve only the queried element in an object array in MongoDB collection
(18 answers)
Closed 4 years ago.
A document in my collection looks like below:
{
"_id":"<uuid>",
"tests":[
{
"min":5,
"max":20
},
{
"min":15,
"max":35
}
]
}
Now, I want to find all the tests in which min >= 5.
db.coll.aggregate([
{ $match: { 'tests.min': {$gte : 5}} },
{ '$unwind': "$tests" }
]);
the above query somehow doesnt return the correct results.
You do not need to write a aggregate query for this. Use the $elemMatch operator for this. Your query will look like as shown
db.coll.find({
tests: {
$elemMatch: {
min: {
$gte: 5
}
}
}
})
The above query will return the desired result.
For more details visit
$elemMatch (query)

mongodb update all document with incrementing the value by 1 [duplicate]

This question already has an answer here:
Multiple $inc updates in MongoDB
(1 answer)
Closed 4 years ago.
i have several document in mongoDB, doc structure is like this
{
"a":"abc",
"myid":2
}
I want to update "myid" of all document with 1. for example for 1st document myid = 1, for second document myid =2 and so on. Is there a query?
Try this query:
db.collection.updateMany(
{},
{$inc:{myid:1}}
)
Create a separate Collection called Counter to track the last sequence number.
db.counters.insert(
{
_id: "userid",
seq: 0
});
And create a function to fetch seq value for Counter collection.
function getNextSequence(name) {
var ret = db.counters.findAndModify(
{
query: { _id: name },
update: { $inc: { seq: 1 } },
new: true
});
return ret.seq;
}
And Increment your auto-increment field like below.
db.collection.insert({
"a":"abc",
"myid": getNextSequence("userid")
});
I hope this will help you.

MongoDB : Update document array value object [duplicate]

This question already has answers here:
How to Update Multiple Array Elements in mongodb
(16 answers)
Closed 5 years ago.
I have document like below
{
_id="12345",
details:[
{
name:"Customer_Name",
time:"request_time"
},
{
name:"Customer_Name",
time:"request_time"
},
{
name:"Customer_Name",
time:"request_time"
}, ....
]
}
I want to update "name" field in all the objects of "details" array.
I can use it update each array object using
db.customer.updateMany({_id:"12345"},{$set:{"details.0.name":"My_Name"}});
Is there any way to update all of them at once
Try:
db.customer.find({ _id:"12345" })
.forEach(function (doc) {
doc.details.forEach(function (details) {
details.name="My_Name";
});
db.customer.save(doc);
});