使用 mongodb 使用对象 id 更新单个数组元素中的多个数据

发布于 2025-01-10 02:26:25 字数 1420 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

刘备忘录 2025-01-17 02:26:25

我认为你可以使用 $ 运算符来执行此操作。

位置 $ 运算符标识数组中要更新的元素,而无需显式指定该元素在数组中的位置。

现在您有两个选择,要么更新各个字段(仅更新您想要的字段),要么更新整个对象。对于它们两者,您都需要 $set

只需将查询作为 update 查询的第一个参数传递,第二个参数是您的 update 子句。

Collection.update({'devices.device_name' : 'Test456356'}, {
{ '$set': { 'devices.

注意:上面更新了查询中找到的所有元素。如果您想具体一点,可以使用 updateOne。只需将 update 替换为 updateOne 它将更新第一个匹配的文档

: { "ime_number" : "888844442222", "device_name" : "Remote Device", "subscription_type" : "Monthly", "validity" : 30, } } } });

注意:上面更新了查询中找到的所有元素。如果您想具体一点,可以使用 updateOne。只需将 update 替换为 updateOne 它将更新第一个匹配的文档

I think you can use $ operator to do this.

The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array.

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.

Collection.update({'devices.device_name' : 'Test456356'}, {
{ '$set': { 'devices.

Note: above updates all the elements found with the query. If you want to be specific you can use updateOne. Just replace update with updateOne and it will update the first matched document

: { "ime_number" : "888844442222", "device_name" : "Remote Device", "subscription_type" : "Monthly", "validity" : 30, } } } });

Note: above updates all the elements found with the query. If you want to be specific you can use updateOne. Just replace update with updateOne and it will update the first matched document

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文