Delete one element from an array in MongoDB? [duplicate] - mongodb

This question already has answers here:
How to remove array element in mongodb?
(6 answers)
Closed 4 years ago.
I have the below document stored in mongo DB collection,I will dynamically receive the url to be removed For eg.,I need to delete the subscribers url http://localhost.8080/FNOL/subscriber1 for the name "name" : "FNOL","country" : "US","lob" : "property" from the document.
How do i write the remove command with mongo?
Do i need to redefine my document structure?
Thanks in advance.
{
"_id" : ObjectId("5b07fbbc0d7a677d2f8b2d87"),
"name" : "FNOL",
"country" : "US",
"lob" : "property",
"subscribers" : [
{
"protocol" : "REST",
"url" : "http://localhost.8080/FNOL/subscriber1"
},
{
"protocol" : "SOAP",
"url" : "http://localhost.8080/FNOL/subscriber2"
},
{
"protocol" : "JMS",
"url" : "NOTIFICATION.TOPIC.FNOL"
}
]
}
After removal:
{
"_id" : ObjectId("5b07fbbc0d7a677d2f8b2d87"),
"name" : "FNOL",
"country" : "US",
"lob" : "property",
"subscribers" : [
{
"protocol" : "SOAP",
"url" : "http://localhost.8080/FNOL/subscriber2"
},
{
"protocol" : "JMS",
"url" : "NOTIFICATION.TOPIC.FNOL"
}
]
}

You can use $pull operator specifying mentioned conditions to get matching document and url as a parameter of $pull like below:
let urlToRemove = "http://localhost.8080/FNOL/subscriber1";
db.col.update(
{ name: "FNOL", country: "US", lob: "property" },
{ $pull: { subscribers: {url: urlToRemove }}})

Related

Mongo query to check whether subobject exist or not

I have a collection invoice with subobject address.'invoiceId' is the key of main object(Invoice) and '130704432ST'is key for address(address subobject is saved as Map(key,value) pair).I need to check whether this subobject exists or not, if exist I need to update this. So while querying it gives no object found for below query. I am using spring Mongotemplate query in my application. So my query looks like below.
Kindly share if you find any issues with this query and also help me with Mongo query to execute in Robo 3T 1.2 for below criteria.
Query: { "_id" : { "$java" : 144068830 }, "addresses.144068830SF" : { "$exists" : true } }, Fields: { }, Sort: { }
{
"_id" : {
"invoiceId" : 130704432
},
"destStoreNbr" : 82,
"invoiceTypeCd" : "M",
"countryCode" : "US",
"invoiceNumber" : "0000000086 ",
"totalAmt" : 1038.76,
"invoiceId" : 130704432
"addresses" : {
"130704432ST" : {
"streetAddress4" : "",
"name" : "kEN-MART",
"streetAddress3" : "",
"invAddrTypeCode" : "ST",
"streetAddress2" : "",
"zipCode" : "310310",
"cityName" : "ATLANTA",
"countryCode" : "US",
"stateCode" : "AL"
}
}

Mongo DB update query [duplicate]

This question already has answers here:
Can you specify a key for $addToSet in Mongo?
(2 answers)
Closed 4 years ago.
I have the below document stored in mongo DB collection,I will need to add new subscribers For eg.,I need to add the subscriber with "protocol" : "SOAP" and url http://localhost.8080/FNOL/subscriber3 for the name "name" : "FNOL","country" : "US","lob" : "property" from the document.
If the url we are adding already exist we shouldn't add to the document,in case if the document does not exist with matching criteria name "name" : "FNOL","country" : "US","lob" : "property" i would need to insert a new document.
is it possible to do all the above in a single command in mongo db?
Thanks in advance.
{
"_id" : ObjectId("5b07fbbc0d7a677d2f8b2d87"),
"name" : "FNOL",
"country" : "US",
"lob" : "property",
"subscribers" : [
{
"protocol" : "REST",
"url" : "http://localhost.8080/FNOL/subscriber1"
},
{
"protocol" : "SOAP",
"url" : "http://localhost.8080/FNOL/subscriber2"
},
{
"protocol" : "JMS",
"url" : "NOTIFICATION.TOPIC.FNOL"
}
]
}
After updation:
{
"_id" : ObjectId("5b07fbbc0d7a677d2f8b2d87"),
"name" : "FNOL",
"country" : "US",
"lob" : "property",
"subscribers" : [
{
"protocol" : "SOAP",
"url" : "http://localhost.8080/FNOL/subscriber2"
},
{
"protocol" : "JMS",
"url" : "NOTIFICATION.TOPIC.FNOL"
},
{
"protocol" : "SOAP",
"url" : "http://localhost.8080/FNOL/subscriber2"
}
,
{
"protocol" : "SOAP",
"url" : "http://localhost.8080/FNOL/subscriber3"
}
]
}
You need to use $addToSet... It will not push the data if it already exist in the array
db.collection.update(
{ name: 'FNOL', country: 'US', lob: 'property' },
{ $addToSet: { subscribers: { "protocol" : "SOAP", url: 'http://localhost.8080/FNOL/subscriber3' } } },
{ upsert: true }
)
Try this.
db.collection.update( { "_id": (id of the object) }, { "$push": {"subscribers": { "url": "(input url )", "protocol": "(input protocol)" } } } )

Update nested array document in mongoDB [duplicate]

This question already has answers here:
Updating a Nested Array with MongoDB
(2 answers)
Closed 5 years ago.
I have mongo document like
{
"_id" : ObjectId("59ad2dbd7549de7c69ef0132"),
"customerId" : "TELEFONICA",
"organization" : "TELEFONICA",
"description" : "",
"events" : [
{
"id" : "abandonCartEvent",
"name" : "Abandoned Cart Event",
"attributes" : [
{
"attrId" : "geoLocation",
"attrName" : "Customer Location",
"defaultValue" : "London"
},
{
"attrId" : "traits",
"attrName" : "Customer Traits",
"defaultValue" : "Sports Lover"
}
]
},
{
"id" : "resumeEvent",
"name" : "Resume Event",
"attributes" : [
{
"attrId" : "geoLocation",
"attrName" : "Customer Location",
"defaultValue" : "London"
},
{
"attrId" : "traits",
"attrName" : "Customer Traits",
"defaultValue" : "Sports Lover"
}
]
},
{
"id" : "cancelEvent",
"name" : "Cancel Event",
"attributes" : [
{
"attrId" : "geoLocation",
"attrName" : "Customer Location",
"defaultValue" : "London"
},
{
"attrId" : "traits_",
"attrName" : "Customer Traits",
"defaultValue" : "Sports Lover"
}
]
}
]
}
Here I want to update "attributes" array, i.e, I need to modify any one object of attributes array.
Can someone provide me solution for updating this nested document
You have to wait for the next version of mongodb to this. Check this.
Untill that time, redesign your document structure in a way so that it has one array(not double nested array) or traverse second array on application level.
Above you given same document I mentioned output scenario,
If are you going to update id : "abandonCartEvent" & attrId : "geoLocation" ,
update({ "customerId" : "TELEFONICA", "events.id":"abandonCartEvent"}, {$set: {"events.0.attributes.0.attrId": "CustLocation"}})
1a. If are you going to update attrId: "traits"
update({ "customerId" : "TELEFONICA", "events.id":"abandonCartEvent"}, {$set: {"events.0.attributes.1.attrId": "traidstTest"}})
If are you going to update Id: "resumeEvent" arrtID ?
update({ "events.id":"resumeEvent"}, {$set: {"events.1.attributes.0.attrId": "GeoLocationSS"}})
Same like if are you updating cancelEvent id,
update({ "events.id":"cancelEvent"}, {$set: {"events.2.attributes.0.attrId": "GeoLocationSTested"}})
I hope, you will understand the update on array fields.
If you have any queries ask me, or call(+91-9964058121)
Thanks

how to match two fields in different lists in mongodb for update query

I need help please, I have to match two embedded document with their ids
for example my document structure is like this:
{
"id" : "00025",
"ean" : "01213134325",
"notify_stock" : NumberLong(10),
"specs" : [
{
"id" : "0005",
"spec" : [
{
"lang" : "fr-FR",
"text" : "taille"
}
],
"value" : [
{
"lang" : "fr-FR",
"text" : "grand"
}
]
}]
}
i have a file for multi language like this
sku_id;spec_id;spec_name;spec_value
00025;0005;size;big
00025;0006;color;black
so my query in mongodb is like this:
Modifier update query:
{
"skus.id" : "<string val>",
"skus.$.specs.id" : "<string val>"
}
Modifier update details:
{
"$push" : {
"skus.$.specs.$.spec" : {
"lang" : "<string val>",
"text" : "<string val>"
},
"skus.$.specs.$.value" : {
"lang" : "<string val>",
"text" : "<string val>"
}
}
}
when i execute this in Pentaho data integration with the step output mongodb, i got an error
"Too many positional (i.e. '$')
elements found in path 'skus.$.specs.$.spec'"

How to generate mongodb object id for a sub-document on insert

Inserting a document that also has sub-documents. I am doing this through cli. How do I make mongodb generate a object id for the Alerts sub-document record? I want something like the following, but this does not work from cli.
db.user.insert(
{
"Email" : "andy+5#domain.com",
"Domain" : "other",
"Type" : "local",
"FName" : "Andy",
"Alerts" : [
{
"_id" : new ObjectId(),
"Keyword" : "sales",
"Frequency" : {
"Type" : "daily"
},
"IsActive" : true
},
{
"_id" : new ObjectId(),
"Keyword" : "customer service",
"Frequency" : {
"Type" : "daily"
},
"IsActive" : true
}
]
}
)
You can run that exact script on the console on robomongo, it would execute just fine. Don't do it on add or edit dialog.