我有以下架构:
var UserSchema = new Schema({
username: { type: String, required: true, unique: true },
displayName: { type: String, required: true },
registered: { type: Boolean, required: true, default: false }
});
Run Code Online (Sandbox Code Playgroud)
然后我尝试使用 findOneAndUpdate 插入和对象:
var user = {
displayName: "Test User"
}
User.findOneAndUpdate({username: "test"}, user, {new: true, upsert: true}, function(err, newUser) {
callback(err, newUser);
});
Run Code Online (Sandbox Code Playgroud)
用户被正确创建(在第一次尝试时,因为它不存在)。但是,注册字段不会设置为 false。当我执行 findOneAndUpdate 时会发生这种情况吗?
mongo 查询的结果:
> db.users.find().pretty()
{
"_id" : ObjectId("56255f415641ace1204de658"),
"username" : "Test",
"displayName" : "Test User"
}
Run Code Online (Sandbox Code Playgroud) 让我们说架构是:
var Rows = mongoose.model('Rows', {
row1: String,
row2: String
});
Run Code Online (Sandbox Code Playgroud)
我如何随机查询其中一行?例如:
var rand = Math.floor(Math.rand() * 2);
Rows.find({ "row" + rand: "sup" }, function (err, result) {
if (err) {
console.log(err);
}
console.log(result);
});
Run Code Online (Sandbox Code Playgroud)
这段代码出错了 SyntaxError: Unexpected token +
我正在尝试通过猫鼬找到原始文档。
Model.collection.find({}, { skip : BATCH*pgi, limit : BATCH }, function(err, docs){
Run Code Online (Sandbox Code Playgroud)
在这里,我期望文档是原始的 mongodb 文档。
但我不这么认为。
如何获取上述案例的原始文件?
mongoose 是否可以创建一个架构,将其命名为 Folders 并且它在被调用的子文件夹中有一个属性,它是一个嵌套的 Folder 子文档数组?
const mongoose = require('mongoose')
let SubSchema = mongoose.Schema({
name: { type: String, required: true }
})
let FolderSchema = mongoose.Schema({
name: { type: String, required: true },
children: [ SubSchema ]
})
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过引用另一个类似于上面显示的模式将子文档嵌套在数组中。不过,我想要做的是在其内部重用 FolderSchema。显然这会导致问题,因为在创建架构时,FolderSchema 不存在。有一个更好的方法吗?有没有办法使用相同的模式递归嵌套文档?
我知道我可以让数组成为引用集合的 ObjectId 列表,但我希望将它全部嵌套为文档。我想如果我确实 populate() 让它解析文档 ID,那本质上是一样的。只是想知道是否还有另一种我不知道的方法。
我想找到正确的子文档并更改特定字段。
这是我的尝试:
Book.findOneAndUpdate(
{ _id: req.params.id, chapters: { $elemMatch: { _id: req.params.subid } } },
{ $set: { chapters: { title: 'new title' } } }, { 'new': true }).exec();
});
Run Code Online (Sandbox Code Playgroud)
还试过:
Book.findOneAndUpdate(
{ _id: req.params.id, 'chapters._id': req.params.subid },
{ $set: { 'title': 'new title' } }, { 'upsert': true }).exec();
Run Code Online (Sandbox Code Playgroud)
第一个解决方案通过用单个字段覆盖所有内容来删除所有内容,title。
我怎样才能正确更新它?
编辑:
//Book
{
"_id": {
"$oid": "5ae2f417fe46cf86900af82f"
},
"chapters": [
{
"_id": {
"$oid": "5af1b798be6bb05e4427ee65"
},
"title": "old title",
"something": "something"
},
//..some more
] …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用嵌套字段对数据进行排序,称为orderIndex.
router.get("/", (req, res) => {
Book.find({ _id: req.params.id })
.sort({ 'Book.chapters.orderIndex': "asc" }) //doesn't work
.then(books => {
res.render("books/index", {
books: books
})
});
});
Run Code Online (Sandbox Code Playgroud)
Book外观示例:
//Book
{
"_id": {
"$oid": "1234517fe46cf86900af82f"
},
"chapters": [
{
"_id": {
"$oid": "a1"
},
"title": "first book",
"orderIndex": "1",
},
{
"_id": {
"$oid": "5678798be6bb05e4427ee65"
},
"title": "second book",
"orderIndex": "2",
},
//..some more
]
}
Run Code Online (Sandbox Code Playgroud) 有没有人知道MongoDB(或者也许是Mongoose保存)生成的"_id"意味着它在中间包含"坏"字样.
eg: 521b633274bad76823000002
Run Code Online (Sandbox Code Playgroud)
我在网上或这里找不到答案.
这发生在"保存"上,但只在我想"更新"文档时才发现.它不允许更新.
db.collection.update(...
Run Code Online (Sandbox Code Playgroud)
尝试"更新"对象的回调始终为null,文档未更新.
切换我的数据库解决了这个问题,所以我相信这不在代码中(并且代码非常简单)
在Node上同时使用mongojs和mongoose.
非常感谢,
抢
作为我的第一个真正的 MERN 项目,我正在构建一个留言板。我目前正在开发一个节点路由来请求板名称及其相关的帖子数,但我遇到了一个问题。我没有得到我需要的值,而是收到信息告诉我有一个待处理的承诺,这看起来很奇怪,因为我正在使用 async/await。这是函数:
exports.postsPerBoard = async (req, res) => {
try {
const boards = await Board.find();
const postCount = boards.map(async (boardI) => {
const posts = await Post.find({ board: boardI.slug });
return [boardI.slug, posts.length];
});
console.log(postCount);
res.send(postCount);
} catch (err) {
console.error(err.message);
res.status(500).send('server error');
}
};
Run Code Online (Sandbox Code Playgroud)
这是控制台日志的结果:
[0] [
[0] Promise { <pending> },
[0] Promise { <pending> },
[0] Promise { <pending> },
[0] Promise { <pending> },
[0] Promise { <pending> }
[0] ]
Run Code Online (Sandbox Code Playgroud)