如何创建“触发器"?在MongoDB中 [英] How to create "trigger" in MongoDB

查看:360
本文介绍了如何创建“触发器"?在MongoDB中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个触发器,在该触发器中,插入的每个子文档将在其他集合中增加一个字段,以生成该集合的子文档计数.

I would like of create a trigger where, to each subdocument inserted would increment in other collection a field, for generate a count of subdocuments that collection.

我尝试使用MapReduce创建搜索,但是对于Milions of Registries来说非常慢.

I tried create a search using MapReduce, but for Milions of the Registries is very slow.

注意:我使用C#,但是如果您喜欢在Bson中演示如何做,没问题.

public class Header
{
    public Header()
    {
        Operation= new List<Operation>();
    }

    public ObjectId Id { get; set; }
    public Int64 Code1 {get; set;}
    public Int64 Code2 {get; set;}
    public string Name { get; set; }
    public List<Operation> Operations { get; set; }
}

public class Operation
{
    public Operation()
    {
        Itens = new List<Item>();
    }

    public string Value { get; set; }
    public List<Item> Item { get; set; }
}

public class Item
{
    public string Value { get; set; }
}

推荐答案

MongoDB没有触发器.您将必须通过插入文档在应用程序中实现此功能,插入成功后,您将使用 $ add运算符来增加其他文档中的字段.

MongoDB has no triggers. You will have to implement this in your application by inserting the document and when the insert was successful, you use the $add operator to increment the field in the other document.

这篇关于如何创建“触发器"?在MongoDB中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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