使用 mongodb 使用对象 id 更新单个数组元素中的多个数据
我是 mongodb 的初学者。我有包含多个数组元素的 mongodb 数据,只需更新单个数组元素中的多个数据
"_id" : ObjectId("6217138f0607ea2e07e70b25"),
"phone_number" : "7645645676",
"email" : "[email protected]",
"name" : "John",
"address" : "44th street Englan",
"devices" : [
{
"ime_number" : "123456789012345",
"device_name" : "Test456356",
"subscription_type" : "Yearly",
"validity" : 365,
"_id" : ObjectId("6217138f0607ea2e07e70b26"),
},
{
"ime_number" : "HHH555",
"device_name" : "Harry Potter",
"subscription_type" : "Monthly",
"validity" : 32,
"_id" : ObjectId("621714570607ea2e07e70b2b"),
}
]
,仅此数据
"ime_number" : "123456789012345",
"device_name" : "Test456356",
"subscription_type" : "Yearly",
"validity" : 365,
已更改数据
"ime_number" : "888844442222",
"device_name" : "Remote Device",
"subscription_type" : "Monthly",
"validity" : 30,
帮助我解决此问题
I am Very Beginner in mongodb.I have mongodb data with multiple array elements just update multiple data in single array element
"_id" : ObjectId("6217138f0607ea2e07e70b25"),
"phone_number" : "7645645676",
"email" : "[email protected]",
"name" : "John",
"address" : "44th street Englan",
"devices" : [
{
"ime_number" : "123456789012345",
"device_name" : "Test456356",
"subscription_type" : "Yearly",
"validity" : 365,
"_id" : ObjectId("6217138f0607ea2e07e70b26"),
},
{
"ime_number" : "HHH555",
"device_name" : "Harry Potter",
"subscription_type" : "Monthly",
"validity" : 32,
"_id" : ObjectId("621714570607ea2e07e70b2b"),
}
]
Only This data
"ime_number" : "123456789012345",
"device_name" : "Test456356",
"subscription_type" : "Yearly",
"validity" : 365,
Changed data
"ime_number" : "888844442222",
"device_name" : "Remote Device",
"subscription_type" : "Monthly",
"validity" : 30,
Help me to solve this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你可以使用
$
运算符来执行此操作。现在您有两个选择,要么更新各个字段(仅更新您想要的字段),要么更新整个对象。对于它们两者,您都需要
$set
:只需将查询作为
update
查询的第一个参数传递,第二个参数是您的 update 子句。注意:上面更新了查询中找到的所有元素。如果您想具体一点,可以使用
updateOne
。只需将update
替换为updateOne
它将更新第一个匹配的文档I think you can use
$
operator to do this.Now you have two options, to either update the individual fields (only the ones you want to) or update the whole object. For both of them you need
$set
:Just pass the query as first argument of your
update
query and the second argument is your update clause.Note: above updates all the elements found with the query. If you want to be specific you can use
updateOne
. Just replaceupdate
withupdateOne
and it will update the first matched document