UPSERT数组元素的匹配条件的MongoDB的文件? [英] Upsert Array Elements matching criteria in a MongoDB document?

查看:292
本文介绍了UPSERT数组元素的匹配条件的MongoDB的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为每个<一href=\"http://stackoverflow.com/questions/7967184/how-do-i-update-array-elements-matching-criteria-in-a-mongodb-document\">How如何更新数组元素的MongoDB的文件中匹配标准?

我要UPSERT数组元素,因此如果一个犯规的比赛再插入,否则更新。

I want to upsert the array elements, so if one doesnt match then insert it, otherwise update it.

我想对这个问题的答案,如果数组元素已经存在,它工作正常。如果元素犯规存在,那么它会创建阵列场下$的孩子。

I tried the answer on that question, and it works fine IF the array element already exists. If the element doesnt exist then it creates a child of "$" under the array field.

我的蒙戈结构如下:

Widget (collection)
--Name
--Properties (array)
  --Name
  --Value

我的应用程序获取窗口小部件名称,并从Web服务调用属性的列表。我想重复提供的属性,如果已存在该名称更新MongoDB的价值,或插入新的属性到属性数组,如果它不。

My application gets a Widget Name and a list of Properties from a WebService call. I wish to iterate the provided Properties and update the value in the MongoDB if the Name already exists, OR insert a new Property to the Properties array if it doesnt.

推荐答案

您需要什么是不可能使用一个单一的更新没有一些应用程序端逻辑。需要注意的是UPSERT因为除非你想如果没有与提供的名称存在自动创建全新的Widget文档的功能是不相关的特定问题。

What you require is not possible using a single update without some app-side logic. Note that upsert as a feature is not relevant for this specific problem unless you want to automatically create new Widget documents if none exist with the provided name.

您正在运行到的问题是,有没有功能,让您可以根据数组元素的存在,做两个不同的更新。你只有两个选择分别是:

The problem you're running into is that there is no functionality that allows you to do two different updates depending on the existence of an array element. Your only two options are :


  1. 找到该项目,确定相关propertie(S)的存在,你的新编译相应的更新或更改属性和执行它。这种带有重要的缺点,这是不是并发安全的方法。换句话说,如果两个Web服务尝试此同一人能覆盖海誓山盟的变化。

  2. 请控件属性顶层文件而不是嵌入。让您用upserts做你想要什么。显而易见的缺点是,这是不是在架构设计方面一个非常不错的选择。如果你取一个小部件,例如,你不会自动获得所有属性。

这篇关于UPSERT数组元素的匹配条件的MongoDB的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆