node.js - Mongodb:仅当该对象的元素唯一时才将对象插入数组
问题描述
我有这个收藏。我试图将 object(item = {label: "Chrome", value: "chrome"}) 添加到数组中,只有当它value
是唯一的时,即插入整个对象,只有当数组item
没有任何其他时与 具有相同值的对象,使用一个 MongoDB 操作foo
foo
item
foo=[{label:"IE",value:"ie"},{label:"Firefox",value:"firefox"}]
解决方案
我不认为 $addToSet 支持您想要的对象字段的重复检测。但是你可以这样做:
db.bars.update(
{
"_id": ObjectId("5d3421a6a0100c1e083356e1"),
"foo": {
"$not": {
"$elemMatch": {
"value": "firefox"
}
}
}
},
{
"$push": {
"foo": {
"label": "Fake Fox",
"value": "firefox"
}
}
}
)
首先,您通过 id + 匹配父对象,它不包含“firefox”作为 foo 对象数组中的值。然后您指定 a$push
将您的新对象添加到 foo 数组。这样,不会在 foo 数组中创建重复项。
不确定您的编码语言是什么,但如果有人感兴趣,这里是生成上述 mongo 查询的 c# 代码:
using MongoDB.Entities;
using System.Linq;
namespace StackOverflow
{
public class Program
{
public class bar : Entity
{
public item[] foo { get; set; }
}
public class item
{
public string label { get; set; }
public string value { get; set; }
}
private static void Main(string[] args)
{
new DB("test");
var bar = new bar
{
foo = new[]
{
new item { label = "IE", value = "ie"},
new item { label = "FireFox", value = "firefox" }
}
};
bar.Save();
var chrome = new item { label = "Chrome", value = "chrome" };
var firefox = new item { label = "Fake Fox", value = "firefox" };
DB.Update<bar>()
.Match(b =>
b.ID == bar.ID && !b.foo.Any(i => i.value == chrome.value))
.Modify(x => x.Push(b => b.foo, chrome))
.Execute();
DB.Update<bar>()
.Match(b => b.ID == bar.ID && !b.foo.Any(i => i.value == firefox.value))
.Modify(x => x.Push(b => b.foo, firefox))
.Execute();
}
}
}
推荐阅读
- java - Java:创建一个后台线程来更新一个变量
- javascript - 如何在javascript中将日期字符串转换为特定的日期格式
- c# - 有没有办法限制 C# 类中的函数只使用特定的签名?
- excel - 双循环自动过滤器,在 Excel VBA 中有效吗?
- pandas - 如何从 Apache Beam 管道中的 pandas.dataFrame 获取输入
- python - 如何在python中将Json对象转换为树类型?
- jquery - 在父级中选择多个类
- angular - 如何使 Bootstrap 表适合页面的可用空间
- android - 删除在 Play 商店中以 alpha 版本发布的应用程序
- wpf - 如何使数据触发器只更新一项