我试图删除嵌套文档“嵌套”有0元素的所有集合。
Tag.deleteMany({ "blog": { $size: 0 } }).exec()出于某种原因,它不适用于猫鼬,但是当我在Robo中运行它时,它就能工作了。
db.getCollection('tags').deleteMany({ "blog": { $size: 0 } })有人知道为什么它在查询shell中工作,但不使用Mongoose代码吗?这是模式。
var tagSchema = new mongoose.Schema({
tag: String,
created: { type: Date, default: Date.now },
blog: [{ type: mongoose.Schema.Types.ObjectId,
ref: "blog" }]
var blogSchema = new mongoose.Schema({
title: String,
image: String,
description: String,
body: String,
created: { type: Date, default: Date.now },
tag:[{ type: mongoose.Schema.Types.ObjectId,
ref: "tag" }]更新..。我想现在是承诺链的问题了吗?
let foundBlog
Blog.findOne({ title: '1st Post' })
.then((blog) => {
foundBlog = blog;
})
.then(() => {
console.log(foundBlog.tag)
Tag.updateMany(
{ _id : { $in: foundBlog.tag} },
{ $pull: { blog: foundBlog._id.toString()} }).exec()
})
.then(() => {
Tag.deleteMany({ "blog": { $size: 0 } }).exec()
})
.then(() => done())由于某种原因,Tag.deleteMany在Tag.updateMany之后没有工作。我的承诺链正确吗?谢谢
发布于 2018-11-30 11:48:11
试着用这样的方式链接:
let foundBlog
Blog.findOne({ title: '1st Post' })
.then((blog) => {
foundBlog = blog;
})
.then(() => {
console.log(foundBlog.tag)
Tag.updateMany(
{ _id : { $in: foundBlog.tag} },
{ $pull: { blog: foundBlog._id.toString()} })
.then(() => {
Tag.deleteMany({ "blog": { $size: 0 } }).then(() => done())
})
})https://stackoverflow.com/questions/53549577
复制相似问题