[英]MongoDB issue with updating nested few times object
Hey guys I have got a problem with updating value of key in the collection using Meteor / Mongo嘿伙计们,我在使用 Meteor / Mongo 更新集合中的键值时遇到了问题
data: { 'id': id, 'fb_id': fbId, 'name': fbName, 'access_token': fbAccessToken, 'symbol': fbSymbol, 'config': { 'get_started': { 'payload': getStarted }, 'persistent_menu': [ { 'locale': 'default', 'call_to_actions': [ { 'type': 'postback', 'title': persistentMenu1, 'payload': 'menu' }, { 'type': 'postback', 'title': persistentMenu2, 'payload': 'knowledge_base' }, { 'type': 'nested', 'title': persistentMenu3, 'call_to_actions': [ { 'type': 'postback', 'title': nestedPersistentMenu1, 'payload': 'subscription' }, { 'type': 'postback', 'title': nestedPersistentMenu2, 'payload': 'tth' } ] } ], 'composer_input_disabled': false }, { 'locale': 'pl_PL', 'call_to_actions': [ { 'type': 'postback', 'title': persistentMenu1, 'payload': 'menu' }, { 'type': 'postback', 'title': persistentMenu2, 'payload': 'knowledge_base' }, { 'type': 'nested', 'title': persistentMenu3, 'call_to_actions': [ { 'type': 'postback', 'title': 'Subskrypcja', 'payload': 'subscription' }, { 'type': 'postback', 'title': 'Konsultant', 'payload': 'tth' } ] } ], 'composer_input_disabled': false } ], 'greeting': [ { 'locale': 'default', 'text': somethingNew }, { 'locale': 'pl_PL', 'text': greetingsText } ] }, 'created_at': '2017-06-05T06:00:37.759455Z', 'updated_at': updatedAt } })
I need to get to second element in "greeting" array and change value of 'text' which is "greetingsText" at the moment我需要进入“greeting”数组中的第二个元素并更改“text”的值,目前为“greetingsText”
I tried to do so using console in the browser by typing this code我尝试通过键入此代码在浏览器中使用控制台来执行此操作
Collection.update({_id: "some_id"}, {$set: {fanpageInfo: {config: {"greeting.2": {text: "tata"}}}}}) but it doesnt work unfortunately
there is error saying "update failed:有错误说“更新失败:
MongoError: The dotted field 'greeting.2' in 'fanpageInfo.config.greeting.2' is not valid for storage."
And to be honest I am not sure if I am targeting it properly - I checked docs and asked google but there are just simple tutorials.老实说,我不确定我的目标是否正确 - 我检查了文档并询问了谷歌,但只有简单的教程。
Thanks in advance for any help在此先感谢您的帮助
First, your greeting
array has only 2 elements, so by using greeting.2
you're attempting to modify third element (zero-based indexes).首先,您的greeting
数组只有 2 个元素,因此通过使用greeting.2
您试图修改第三个元素(从零开始的索引)。
Second, you should use dot notation to modify just one particular field:其次,您应该使用点符号来修改一个特定的字段:
{ $set: { "fanpageInfo.config.greeting.1.text": "tata" } }
I've changed 2
to 1
in this update.在此更新中,我已将2
更改为1
。 If you will attempt to use 2
instead — it will create another document in array with just text: "tata"
inside.如果您尝试使用2
代替 - 它将在数组中创建另一个文档,其中只有text: "tata"
。
FIND QUERY:查找查询:
You need to go through these links as below:您需要通过以下链接:
Meteor Specific find-nth-element-of-array-in-mongo-collection-meteor Meteor 特定find-nth-element-of-array-in-mongo-collection-meteor
Mongo Specific get-n-th-element-of-an-array-in-mongodb Mongo 特定的get-n-th-element-of-an-array-in-mongodb
Simple for finding you need to use slice
like ChatRooms.findOne( {}, { chatIds: { $slice: 1 } } )
;简单的寻找你需要使用slice
像ChatRooms.findOne( {}, { chatIds: { $slice: 1 } } )
UPDATE QUERY更新查询
You can try你可以试试
Collection.update({_id: "some_id"}, {$set: {'config.greeting.1.text': 'tata'}});
If there is anything at all like
fanpageInfo
as parent field ofconfig
then update query shall beCollection.update({_id: "some_id"}, {$set: {'fanpageInfo.config.greeting.1.text': 'tata'}});
如果有像fanpageInfo
这样的fanpageInfo
作为config
父字段,那么更新查询应该是Collection.update({_id: "some_id"}, {$set: {'fanpageInfo.config.greeting.1.text': 'tata'}});
I ran your above data in mongodb and it works fine with above query.我在 mongodb 中运行了您的上述数据,并且在上述查询中运行良好。 Image is below图片如下
For more : Click Here更多:点击这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.