草庐IT

node.js - 在不创建重复项的情况下使用阵列进行Upsert

coder 2023-11-04 原文

我无法“插入”我的阵列。下面的代码在我的answers数组中创建了重复项,我绝对不希望这样做,并且现在看来$push无法正常工作。我尝试使用在SO上看到的不同方法已有一段时间了,但是没有一种方法适合我。使用此网络应用程序,用户可以在网站上查看问题并以"is"或“否”的response进行响应,并且他们可以随时更改(更新)其response,这意味着在数据库在不同的时间。如何解决这个问题?

var QuestionSchema = Schema ({
    title       :String,
    admin       :{type: String, ref: 'User'},
    answers     :[{type:  Schema.Types.Mixed, ref: 'Answer'}]
});

var AnswerSchema = Schema ({
    _question   :{type: ObjectId, ref: 'Question'},
    employee    :{type: String, ref: 'User'},
    response    :String,
    isAdmin     :{type: Boolean, ref: 'User'}
})

var UserSchema = Schema({
    username    : String,
    isAdmin     : {type: Boolean, default: false}
});

module.exports = mongoose.model('Question', QuestionSchema);
module.exports = mongoose.model('Answer', AnswerSchema);
module.exports = mongoose.model('User', UserSchema);


           Question.update(
                {_id: req.body.id},
                {$push: {answers: {_question: req.body.id, 
                                    employee: req.body.employee, 
                                    response: req.body.response, //this variable changes (yes/no/null)
                                     isAdmin: req.body.isAdmin}}},
                {safe: true, upsert: true},
                function(err, model) {

                }
            );

最佳答案

正如我所看到的,您似乎有些困惑,它反射(reflect)在您的模式中。您似乎没有完全掌握“嵌入式”和“引用”之间的区别,因为您的架构实际上是这两种技术的无效“混搭”。

可能最好将它们引导给您。

嵌入式模型

因此,实际上您应该拥有类似以下内容,而不是您已定义的模式:

var QuestionSchema = Schema ({
    title       :String,
    admin       :{type: String, ref: 'User'},
    answers     :[AnswerSchema]
});

var AnswerSchema = Schema ({
    employee    :{type: String, ref: 'User'},
    response    :String,
    isAdmin     :{type: Boolean, ref: 'User'}
})

mongoose.model('Question', questionSchema);

注意:此处Question唯一的实际模型。 AnswerSchema完全“嵌入”。

注意“schema”的明确定义,其中"answers"中的Question属性定义为AnswerSchema的“数组”。这是您进行嵌入并保持对数组内部对象内类型的控制的方式。

至于更新,有一个明确的逻辑模式,但是您根本没有执行它。您需要做的就是“告诉”您不想“推送”新项目的更新,如果该数组中已经存在该“唯一” "employee"的内容。

还。这是,而不是和“upsert”。 Upsert暗示“创建一个新的”,这与您想要的不同。您想“推送”到“现有”问题的数组。如果您在此处保留“upsert”,那么找不到的内容将创建一个新的Question。这当然是错误的。
Question.update(
  { 
    "_id": req.body.id,
    "answers.employee": { "$ne": req.body.employee },
    }
  },
  { "$push": {
    "answers": { 
      "employee": req.body.employee, 
      "response": req.body.response,
      "isAdmin": req.body.isAdmin
    }
  }},
  function(err, numAffected) {

  });

这将检查数组成员中的“唯一” "employee"是否已经存在,并且仅在尚未存在的 $push 中进行检查。

另外,如果您打算允许用户“更改他们的答案”,那么我们可以使用 .bulkWrite() 来实现:
Question.collection.bulkWrite(
  [
    { "updateOne": { 
      "filter": {
        "_id": req.body.id,
        "answers.employee": req.body.employee,
      },
      "update": {
        "$set": {
          "answers.$.response": req.body.response,
        }
      }
    }},
    { "updateOne": { 
      "filter": {
        "_id": req.body.id,
        "answers.employee": { "$ne": req.body.employee },
      },
      "update": {
        "$push": {
          "answers": { 
            "employee": req.body.employee, 
            "response": req.body.response,
            "isAdmin": req.body.isAdmin
          }
        }
      }
    }}
  ],
  function(err, writeResult) {

  }
);

这实际上将两个更新合二为一。第一个尝试更改现有答案,并在匹配的位置 $set 响应,第二个尝试在问题上找不到答案的地方添加新答案。

引用模型

使用“引用”模型,您实际上将Answer的真实成员包含在自己的集合中。因此,架构的定义如下:
var QuestionSchema = Schema ({
    title       :String,
    admin       :{type: String, ref: 'User'},
    answers     :[{ type: Schema.Types.ObjectId, ref: 'Answer' }]
});

var AnswerSchema = Schema ({
    _question   :{type: ObjectId, ref: 'Question'},
    employee    :{type: String, ref: 'User'},
    response    :String,
    isAdmin     :{type: Boolean, ref: 'User'}
})

mongoose.model('Answer', answerSchema);
mongoose.model('Question', questionSchema);

N.B 其他引用在这里是User的,例如:
    employee    :{type: String, ref: 'User'},
    isAdmin     :{type: Boolean, ref: 'User'}

这些也确实是不正确的,也应该是Schema.Type.ObjectId,因为它们“引用” _id的实际User字段。但这实际上超出了所提问题的范围,因此,如果您在阅读完此书后仍然不理解该内容,则可以使用Ask a New Question,以便有人可以解释。关于其余的答案。

不过,这就是模式的“一般”形状,重要的是"ref"“model”的'Anwser',该名称由注册名称组成。您可以选择仅将现代 Mongoose 版本中的"_question"字段与“虚拟”一起使用,但是我现在跳过了“高级用法”,并通过Question模型中的一系列“引用”使其保持简单。

在这种情况下,由于Answer模型实际上位于其自己的“集合”中,因此该操作实际上变为“upserts”。当对给定的"employee" id没有"_question"响应时,我们只想“创建”。

还用 Promise 链演示:
Answer.update(
  { "_question": req.body.id, "employee": req.body.employee },
  { 
    "$set": {
      "response": req.body.reponse
    },
    "$setOnInsert": {
      "isAdmin": req.body.isAdmin
    }
  },
  { "upsert": true }
).then(resp => {
  if ( resp.hasOwnProperty("upserted") ) {
    return Question.update(
      { "_id": req.body.id, "answers": { "$ne": resp.upserted[0]._id  },
      { "$push": { "answers": resp.upserted[0]._id  } }
    ).exec()
  }
  return;
}).then(resp => {
   // either undefined where it was not an upsert or 
   // the update result from Question where it was
}).catch(err => { 
   // something
})

这实际上是一条简单的语句,因为“当匹配时”我们要使用请求的有效负载来更改"response"数据,并且实际上仅在“upserting”或“创建/插入”时才实际更改其他数据,例如"employee"(它总是隐含在查询表达式()和"isAdmin"中,这显然不随每个更新请求而改变,然后我们显式使用 $setOnInsert ,因此它在实际的“创建”中写入了这两个字段。

在“ promise 链”中,我们实际上要查看对Answer的更新请求是否实际上导致了“upsert”,并且当它发出时,我们希望将其追加到尚不存在的Question数组中。与“嵌入式”示例大致相同,最好在使用“更新”进行修改之前先查看数组是否确实具有该项目。或者,您可以在此处 $addToSet ,让查询通过Question匹配_id。但是对我来说,那是浪费的写作。

概括

这些是您处理此问题的不同方法。每种都有自己的用例,您可以在以下方面看到我的一些其他答案的概述:
  • Mongoose populate vs object nesting概述了不同的方法及其背后的原因
  • How to Model a “likes” voting system with MongoDB更加详细地介绍了“嵌入式”模型的“唯一数组更新”技术。

  • 并非“必读”阅读,但它可以帮助您深入了解哪种方法最适合您的特定情况。

    工作实例

    复制这些文件并将它们放在目录中,然后执行npm install以安装本地依赖项。该代码将运行并在数据库中创建进行更改的集合。

    使用mongoose.set(debug,true)打开日志记录,因此您应该查看控制台输出并查看其操作,以及结果集合,在该集合中将记录对相关问题的答案,然后覆盖原意,而不是“复制”。

    如果需要,请更改连接字符串。但这就是您应为此目的在列表中更改的所有内容。答案中描述的两种方法都得到了证明。

    package.json
    {
      "name": "colex",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "dependencies": {
        "async": "^2.4.1",
        "mongodb": "^2.2.29",
        "mongoose": "^4.10.7"
      }
    }
    

    index.js
    const async = require('async'),
        mongoose = require('mongoose'),
        Schema = mongoose.Schema,
        ObjectId = require('mongodb').ObjectID
    
    mongoose.Promise = global.Promise;
    mongoose.set('debug',true);
    mongoose.connect('mongodb://localhost/coltest');
    
    const userSchema = new Schema({
      username: String,
      isAdmin: { type: Boolean, default: false }
    });
    
    const answerSchemaA = new Schema({
      employee: { type: Schema.Types.ObjectId, ref: 'User' },
      response: String,
    });
    
    const answerSchemaB = new Schema({
      question: { type: Schema.Types.ObjectId, ref: 'QuestionB' },
      employee: { type: Schema.Types.ObjectId, ref: 'User' },
      response: String,
    });
    
    const questionSchemaA = new Schema({
      title: String,
      admin: { type: Schema.Types.ObjectId, ref: 'User' },
      answers: [answerSchemaA]
    });
    
    const questionSchemaB = new Schema({
      title: String,
      admin: { type: Schema.Types.ObjectId, ref: 'User' },
      answers: [{ type: Schema.Types.ObjectId, ref: 'AnswerB' }]
    });
    
    const User = mongoose.model('User', userSchema);
    
    const AnswerB = mongoose.model('AnswerB', answerSchemaB);
    
    const QuestionA = mongoose.model('QuestionA', questionSchemaA);
    const QuestionB = mongoose.model('QuestionB', questionSchemaB);
    
    
    async.series(
      [
        // Clear data
        (callback) => async.each(mongoose.models,(model,callback) =>
          model.remove({},callback),callback),
    
        // Create some data
        (callback) =>
          async.each([
            {
              "model": "User",
              "object": {
                "_id": "594a322619ddbd437193c759",
                "name": "Admin",
                "isAdmin": true
              }
            },
            {
              "model": "User",
              "object": {
                "_id":  "594a323919ddbd437193c75a",
                "name": "Bill"
              }
            },
            {
              "model": "User",
              "object": {
                "_id":  "594a327b19ddbd437193c75b",
                "name": "Ted"
              }
            },
            {
              "model": "QuestionA",
              "object": {
                "_id": "594a32f719ddbd437193c75c",
                "admin": "594a322619ddbd437193c759",
                "title": "Question A Model"
              }
            },
            {
              "model": "QuestionB",
              "object": {
                "_id": "594a32f719ddbd437193c75c",
                "admin": "594a322619ddbd437193c759",
                "title": "Question B Model"
              }
            }
          ],(data,callback) => mongoose.model(data.model)
            .create(data.object,callback),
          callback
        ),
    
        // Submit Answers for Users - Question A
        (callback) =>
          async.eachSeries(
            [
              {
                "_id": "594a32f719ddbd437193c75c",
                "employee": "594a323919ddbd437193c75a",
                "response": "Bills Answer"
              },
              {
                "_id": "594a32f719ddbd437193c75c",
                "employee": "594a327b19ddbd437193c75b",
                "response": "Teds Answer"
              },
              {
                "_id": "594a32f719ddbd437193c75c",
                "employee": "594a323919ddbd437193c75a",
                "response": "Bills Changed Answer"
              }
            ].map(d => ([
              { "updateOne": {
                "filter": {
                  "_id": ObjectId(d._id),
                  "answers.employee": ObjectId(d.employee)
                },
                "update": {
                  "$set": { "answers.$.response": d.response }
                }
              }},
              { "updateOne": {
                "filter": {
                  "_id": ObjectId(d._id),
                  "answers.employee": { "$ne": ObjectId(d.employee) }
                },
                "update": {
                  "$push": {
                    "answers": {
                      "employee": ObjectId(d.employee),
                      "response": d.response
                    }
                  }
                }
              }}
            ])),
            (data,callback) => QuestionA.collection.bulkWrite(data,callback),
            callback
          ),
    
        // Submit Answers for Users - Question A
        (callback) =>
          async.eachSeries(
            [
              {
                "_id": "594a32f719ddbd437193c75c",
                "employee": "594a323919ddbd437193c75a",
                "response": "Bills Answer"
              },
              {
                "_id": "594a32f719ddbd437193c75c",
                "employee": "594a327b19ddbd437193c75b",
                "response": "Teds Anwser"
              },
              {
                "_id": "594a32f719ddbd437193c75c",
                "employee": "594a327b19ddbd437193c75b",
                "response": "Ted Changed it"
              }
            ],
            (data,callback) => {
              AnswerB.update(
                { "question": data._id, "employee": data.employee },
                { "$set": { "response": data.response } },
                { "upsert": true }
              ).then(resp => {
                console.log(resp);
                if (resp.hasOwnProperty("upserted")) {
                  return QuestionB.update(
                    { "_id": data._id, "employee": { "$ne": data.employee } },
                    { "$push": { "answers": resp.upserted[0]._id } }
                  ).exec()
                }
                return;
              }).then(() => callback(null))
              .catch(err => callback(err))
            },
            callback
          )
      ],
      (err) => {
        if (err) throw err;
        mongoose.disconnect();
      }
    )
    

    关于node.js - 在不创建重复项的情况下使用阵列进行Upsert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44660284/

    有关node.js - 在不创建重复项的情况下使用阵列进行Upsert的更多相关文章

    1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

      我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

    2. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

      我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

    3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

      类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

    4. ruby-on-rails - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

      很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

    5. ruby - 在 Ruby 中使用匿名模块 - 2

      假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于

    6. ruby - 如何在 Ruby 中顺序创建 PI - 2

      出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

    7. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

      我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

    8. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

      关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

    9. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

      我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

    10. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

      我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

    随机推荐