确认 $addToSet 添加一个元素 [英] Confirm $addToSet adds an element

查看:43
本文介绍了确认 $addToSet 添加一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数通过 $addToSet 将一堆数据添加到 db 中,我需要确认数据是否已添加.由于 $addToSet 不添加重复项,我想知道是否未添加重复项(向用户显示错误)或是否添加了 db 条目(向用户显示确认).

I have a function adding a bunch of data into db through $addToSet, and I require a confirmation if the data has been added or not. Since $addToSet does not add duplicates, I want to know if a duplicate has not been added (show an error to the user) or if a db entry has been added (show confirmation to user).

我基本上需要 $addToSet 操作的回调.在文档中找不到它.mongodb 新手.希望得到帮助.

I basically need a callback for the $addToSet operation. Couldnt find it in the docs. New to mongodb. Would appreciate help.

_notifications.update(
        {'username': data.username}, 
        { $addToSet: pushNotification }, function(err, docs){
            console.log(docs);
            if (docs == 0){
                co_notifications.insert(
                    {'username': data.username, 'notifications': insertNotification}, function(er, doc){
                });
            }
        },
        { upsert: true }
    );

推荐答案

我可能遗漏了一些东西,但我唯一能真正看到的是新的批处理操作 API 的结果.

I may be missing something but the only thing I can really see is in the result of the new Batch operations API.

var mongodb = require('mongodb'),
    MongoClient = mongodb.MongoClient;


MongoClient.connect("mongodb://localhost/test",function(err,db) {

  var col = db.collection("mytest");
  var batch = col.initializeOrderedBulkOp();


  batch.find({ "user": "me" }).upsert().updateOne({ "$addToSet": { "list": 5 } });
  batch.execute(function(err,result) {
    console.log( JSON.stringify( result, undefined, 4 ) );
  });

});

在您第一次执行result"的内容时,在该示例列表中会像这样转储:

On that sample listing the first time you execute the contents of "result" will dump like this:

{
    "ok": 1,
    "writeErrors": [],
    "writeConcernErrors": [],
    "nInserted": 0,
    "nUpserted": 1,
    "nMatched": 0,
    "nModified": 0,
    "nRemoved": 0,
    "upserted": [
        {
            "index": 0,
            "_id": "5397ae995f04804cbeb7c663"
        }
    ]
}

值得注意的键是nUpserted"和每个创建的文档的upserted"数组.虽然我们只做了一次,但毕竟是批量操作.

The notable keys there being "nUpserted" and the "upserted" array for each document that was created. It is a batch operation afterall even though we are doing it once.

在第二次执行时,它应该找到现有文档并匹配 set 成员,你会得到这个:

On the second execution where it should find the existing document and also match the set member you would get this:

{
    "ok": 1,
    "writeErrors": [],
    "writeConcernErrors": [],
    "nInserted": 0,
    "nUpserted": 0,
    "nMatched": 1,
    "nModified": 0,
    "nRemoved": 0,
    "upserted": []
}

这表明,虽然匹配了文档,但实际上没有修改任何内容,因为 set 成员是相同的.但是,如果您将 $addToSet 操作中应用的值更改为不存在的值,那么响应将是这样的:

That shows that while the document was matched nothing was actually modified as the set member was the same. But if you change the value of what is applied in the $addToSet operation to something that doesn't exist then the response would be this:

{
    "ok": 1,
    "writeErrors": [],
    "writeConcernErrors": [],
    "nInserted": 0,
    "nUpserted": 0,
    "nMatched": 1,
    "nModified": 1,
    "nRemoved": 0,
    "upserted": []
}

这同时显示了nMatched"和nModified"值,表明文档实际上是由 $addToSet 操作更新的.

This shows both the "nMatched" and "nModified" values indicating the document was actually updated by the $addToSet operation.

这可能是一种可能的方法

So that may be a possible approach

这篇关于确认 $addToSet 添加一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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