小编Hor*_*ace的帖子

当父文档可能不存在时更新MongoDB子文档

这是我的数据,包括书籍收藏,以及books.reviews子集合.

books = [{
    _id: ObjectId("5558f40ad498c653748cf045"),
    title: "Widget XYZ",
    isbn: 1234567890,
    reviews: [
        {
            _id: ObjectId("5558f40ad498c653748cf046"),
            userId: "01234",
            rating: 5,
            review: "Yay, this great!"
        },
        {
            _id: ObjectId("5558f40ad498c653748cf047"),
            userId: "56789",
            rating: 3,
            review: "Meh, this okay."
        }
    ]
}]
Run Code Online (Sandbox Code Playgroud)

在Node.js中(使用Mongoose),我需要用户能够通过表单提交评论,将评论和书籍的isbn发送到服务器,具有以下条件:

  1. 如果该书已经不存在特定的isbn,请添加它,然后添加评论(显然它尚不存在).
  2. 如果这本书确实存在......
    • 如果此用户对本书不存在评论,请添加该评论.
    • 如果审查并为这本书存在这个用户,更新.

我可以通过4个单独的查询来完成此操作,但我知道这不是最佳的.我可以使用的查询数量最少(当然,这些查询是什么)?

mongodb node.js subdocument

5
推荐指数
1
解决办法
706
查看次数

ESLint 错误:行号超出范围

我在标准配置中使用 ESLint。

运行时出现此错误eslint --fix */**

Line number out of range (line 318 requested, but only 317 lines present).
RangeError: Line number out of range (line 318 requested, but only 317 lines present).
    at SourceCode.getIndexFromLoc (/usr/local/lib/node_modules/eslint/lib/util/source-code.js:389:19)
    at Object.fix (/usr/local/lib/node_modules/eslint/lib/rules/no-multiple-empty-lines.js:116:52)
    at RuleContext.report (/usr/local/lib/node_modules/eslint/lib/rule-context.js:127:34)
    at Program:exit.allLines.reduce.concat.reduce (/usr/local/lib/node_modules/eslint/lib/rules/no-multiple-empty-lines.js:108:37)
    at Array.reduce (native)
    at EventEmitter.Program:exit (/usr/local/lib/node_modules/eslint/lib/rules/no-multiple-empty-lines.js:93:22)
    at emitOne (events.js:101:20)
    at EventEmitter.emit (events.js:191:7)
    at NodeEventGenerator.applySelector (/usr/local/lib/node_modules/eslint/lib/util/node-event-generator.js:265:26)
    at NodeEventGenerator.applySelectors (/usr/local/lib/node_modules/eslint/lib/util/node-event-generator.js:294:22)
Run Code Online (Sandbox Code Playgroud)

任何想法如何解决这一问题?

standard --fix首先从标准 CLI尝试并得到相同的错误,然后直接从 ESLint 尝试以确认这是后者抛出的错误,而不是前者。

javascript eslint

3
推荐指数
1
解决办法
1268
查看次数

标签 统计

eslint ×1

javascript ×1

mongodb ×1

node.js ×1

subdocument ×1