I am trying to update embedded document in mongodb using $set. Gives error write error Cannot create field 'category' in element {: null} if update array of json.
Initially document:
db.testdata.find().pretty()
{
"_id" : ObjectId("5afe722a8a72eab1deb77a99"),
"device_type" : "pharmacy",
"parameters" : [{
"category" : "Azelastine and Fluticasone",
"code" : "P7852334",
"description" : "Seasonal allergic rhinitis",
"subcategory" : ""
},
{
"category" : "Azithromycin",
"code" : "P7852336",
"description" : "Bacterial infections of respiratory tract",
"subcategory" : ""
} ]
}
if replace and add few elements in parameter array it works:
db.testdata.update({ device_type: "pharmacy" }, { $set: { "parameters.0.code":"P7852342","parameters.0.category":"Azelastine and Fluticasone","parameters.0.subcategory":"","parameters.0.description":"Seasonal allergic rhinitis","parameters.1.code":"P7852343","parameters.1.category":"Azithromycin","parameters.1.subcategory":"","parameters.1.description":"Bacterial infections of respiratory tract","parameters.2.code":"P7852344","parameters.2.category":"Azilsartan Medoxomil","parameters.2.subcategory":"","parameters.2.description":"Hypertension","parameters.3.code":"P7852345","parameters.3.category":"Cetrizine Hydrochloride.","parameters.3.subcategory":"","parameters.3.description":"Cold and allergy","parameters.4.code":"P7852346","parameters.4.category":"Artemether and Lumefantrine","parameters.4.subcategory":"","parameters.4.description":"Malaria","parameters.5.code":"P7852347","parameters.5.category":"Aspirin","parameters.5.subcategory":"","parameters.5.description":"Pain and inflammation" } })But unable to add larger array like 14 elements:
db.testdata.update({ "device_type" : "pharmacy" }, { $set: {"parameters.0.code":"P7852342","parameters.0.category":"Azelastine and Fluticasone","parameters.0.subcategory":"","parameters.0.description":"Seasonal allergic rhinitis","parameters.1.code":"P7852343","parameters.1.category":"Azithromycin","parameters.1.subcategory":"","parameters.1.description":"Bacterial infections of respiratory tract","parameters.2.code":"P7852344","parameters.2.category":"Azilsartan Medoxomil","parameters.2.subcategory":"","parameters.2.description":"Hypertension","parameters.3.code":"P7852345","parameters.3.category":"Cetrizine Hydrochloride.","parameters.3.subcategory":"","parameters.3.description":"Cold and allergy","parameters.4.code":"P7852346","parameters.4.category":"Artemether and Lumefantrine","parameters.4.subcategory":"","parameters.4.description":"Malaria","parameters.5.code":"P7852347","parameters.5.category":"Aspirin","parameters.5.subcategory":"","parameters.5.description":"Pain and inflammation","parameters.6.code":"P7852348","parameters.6.category":"Aripiprazole","parameters.6.subcategory":"","parameters.6.description":"Schizophrenia and depression","parameters.7.code":"P7852349","parameters.7.category":"Bambuterol","parameters.7.subcategory":"","parameters.7.description":"Chronic bronchial asthma","parameters.8.code":"P7852350","parameters.8.category":"Beclomethasone Dipropionate","parameters.8.subcategory":"","parameters.8.description":"Corticosteroid and anti-inflammatory","parameters.9.code":"P7852351","parameters.9.category":"Benazepril","parameters.9.subcategory":"","parameters.9.description":"High blood pressure","parameters.10.code":"P7852352","parameters.10.category":"Desipramine","parameters.10.subcategory":"","parameters.10.description":"Antidepressant","parameters.11.code":"P7852353","parameters.11.category":"Diazepam","parameters.11.subcategory":"","parameters.11.description":"Anti anxiety","parameters.12.code":"P7852354","parameters.12.category":"Dicycloverine Hydrochloride","parameters.12.subcategory":"","parameters.12.description":"Gastrointestinal tract spasm","parameters.13.code":"P7852355","parameters.13.category":"Dilazep","parameters.13.subcategory":"","parameters.13.description":"Coronary heart disease","parameters.14.code":"P7852356","parameters.14.category":"Metaraminol","parameters.14.subcategory":"","parameters.14.description":"Spinal anesthesia","parameters.15.code":"P7852357","parameters.15.category":"Methenamine Hippurate","parameters.15.subcategory":"","parameters.15.description":"Urinary tract infections","parameters.16.code":"P7852358","parameters.16.category":"Valbenazine","parameters.16.subcategory":"","parameters.16.description":"Nervous system disorder","parameters.17.code":"P7852359","parameters.17.category":"Vasopressin","parameters.17.subcategory":"","parameters.17.description":"Control of variceal bleeding","parameters.18.code":"P7852360","parameters.18.category":"Warfarin","parameters.18.subcategory":"","parameters.18.description":"Deep venous thrombosis and pulmonary embolism","parameters.19.code":"P7852361","parameters.19.category":"Zolmitriptan","parameters.19.subcategory":"","parameters.19.description":"Migraine","parameters.20.code":"P7852362","parameters.20.category":"Zuclopenthixol","parameters.20.subcategory":"","parameters.20.description":"Mental health disorder","parameters.21.code":"P7852363","parameters.21.category":"Papaverine","parameters.21.subcategory":"","parameters.21.description":"Smooth muscle spasm, cerebral and peripheral ischemia","parameters.22.code":"P7852364","parameters.22.category":"Perindopril","parameters.22.subcategory":"","parameters.22.description":"High blood pressure, heart attack and heart failure","parameters.23.code":"P7852365","parameters.23.category":"Phenazoppyridine","parameters.23.subcategory":"","parameters.23.description":"Urinay tract analgesic" } })
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 28,
"errmsg" : "Cannot create field 'category' in element {: null}"
}
})Hi
For the update operation you’re doing, it’s best not to depend on the dot notation syntax for the $set operation, because it can create ambiguity.
For example, if the document doesn’t have the named field:
> db.test2.find()
{
"_id": 0
}
> db.test2.update({_id:0}, {$set: {"parameters.0.code": "123", "parameters.0.category": "456"}})
Updated 1 existing record(s) in 2ms
WriteResult({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.test2.find()
{
"_id": 0,
"parameters": {
"0": {
"category": "456",
"code": "123"
}
}
}
Note that instead of creating an array inside the parameters field, it created a sub-document with 0 as key instead.
This would be quite different if the parameters field exists with an empty array:
> db.test3.find()
{
"_id": 0,
"parameters": [ ]
}
> db.test3.update({_id:0}, {$set: {"parameters.0.code": "123", "parameters.0.category": "456"}})
Updated 1 existing record(s) in 2ms
WriteResult({"nMatched": 1, "nUpserted": 0, "nModified": 1})
> db.test3.find()
{
"_id": 0,
"parameters": [
{
"category": "456",
"code": "123"
}
]
}
In terms of your query, the following syntax should work:
db.testdata.update({ "device_type" : "pharmacy" }, { $set: {"parameters": [{"code": "P7852342", "category": "Azelastine and Fluticasone", "subcategory": "", "description": "Seasonal allergic rhinitis"}, {"code": "P7852343", "category": "Azithromycin", "subcategory": "", "description": "Bacterial infections of respiratory tract"}, {"code": "P7852344", "category": "Azilsartan Medoxomil", "subcategory": "", "description": "Hypertension"}, {"code": "P7852345", "category": "Cetrizine Hydrochloride.", "subcategory": "", "description": "Cold and allergy"}, {"code": "P7852346", "category": "Artemether and Lumefantrine", "subcategory": "", "description": "Malaria"}, {"code": "P7852347", "category": "Aspirin", "subcategory": "", "description": "Pain and inflammation"}, {"code": "P7852348", "category": "Aripiprazole", "subcategory": "", "description": "Schizophrenia and depression"}, {"code": "P7852349", "category": "Bambuterol", "subcategory": "", "description": "Chronic bronchial asthma"}, {"code": "P7852350", "category": "Beclomethasone Dipropionate", "subcategory": "", "description": "Corticosteroid and anti-inflammatory"}, {"code": "P7852351", "category": "Benazepril", "subcategory": "", "description": "High blood pressure"}, {"code": "P7852352", "category": "Desipramine", "subcategory": "", "description": "Antidepressant"}, {"code": "P7852353", "category": "Diazepam", "subcategory": "", "description": "Anti anxiety"}, {"code": "P7852354", "category": "Dicycloverine Hydrochloride", "subcategory": "", "description": "Gastrointestinal tract spasm"}, {"code": "P7852355", "category": "Dilazep", "subcategory": "", "description": "Coronary heart disease"}, {"code": "P7852356", "category": "Metaraminol", "subcategory": "", "description": "Spinal anesthesia"}, {"code": "P7852357", "category": "Methenamine Hippurate", "subcategory": "", "description": "Urinary tract infections"}, {"code": "P7852358", "category": "Valbenazine", "subcategory": "", "description": "Nervous system disorder"}, {"code": "P7852359", "category": "Vasopressin", "subcategory": "", "description": "Control of variceal bleeding"}, {"code": "P7852360", "category": "Warfarin", "subcategory": "", "description": "Deep venous thrombosis and pulmonary embolism"}, {"code": "P7852361", "category": "Zolmitriptan", "subcategory": "", "description": "Migraine"}, {"code": "P7852362", "category": "Zuclopenthixol", "subcategory": "", "description": "Mental health disorder"}, {"code": "P7852363", "category": "Papaverine", "subcategory": "", "description": "Smooth muscle spasm, cerebral and peripheral ischemia"}, {"code": "P7852364", "category": "Perindopril", "subcategory": "", "description": "High blood pressure, heart attack and heart failure"}, {"code": "P7852365", "category": "Phenazoppyridine", "subcategory": "", "description": "Urinay tract analgesic"} ]}})
Having said that, is there a reason why you’re using such a large array?
For schema design considerations, you may find the following links helpful:
Best regards
Kevin
> db.test3.find()
{ "_id": 0, "parameters": [{ "code": "1", "category": "category 1" }, { "code": "2", "category": "category 2" } ] }
> db.test3.update({ _id:0 }, { $set: { parameters: [{ "code": "3", "category": "3" }] }})
{ "_id": 0, "parameters": [{ "code": "3", "category": "3" } ] }
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/9439a782-95f9-4efb-acf9-66da2afbbe7e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hi
But when trying to update using path gives error mongodb write error cannot create field category of null.
Regarding that error, I have submitted SERVER-35220. Please watch the ticket for updates on this issue.
Best regards
Kevin