我正在尝试在我的API端点中创建一个查询,以获取所有文档和子文档。目前,我只有运气来获取每个子文档的ID,而不是获取每个文档中的所有信息。是否可以使用猫鼬(连同父文档中的数据)基于每个ID从每个子文档中提取数据?
我已经按照文档进行了深入填充:https://mongoosejs.com/docs/populate.html#deep-populate,但是运气不佳。
这是我当前的代码,可以“工作”,但仅以每个子文档的ID(和可以使用的用户数据)作为响应。
//The get request
router.get("/allUsers", async (req, res) => {
const allUsers = await User.find()
.populate("Post")
.exec();
res.send(allUsers);
});
//User Schema
const userSchema = Schema({
name: { type: String, required: true },
date: { type: Date, default: Date.now },
posts: [{ type: Schema.Types.ObjectId, ref: "Post" }]
});
//Post Schema
const postSchema = new mongoose.Schema({
name: { type: String, required: true },
description: { type: String, required: true }
date: { type: Date, default: Date.now }
});
我想从子文档中提取所有信息而不是仅从ID中提取什么信息?
答案 0 :(得分:1)
将字段名称传递给populate
,而不是模型名称:
const allUsers = await User.find()
.populate("posts")
.exec();
答案 1 :(得分:0)
在其他填充中使用填充
.populate({
path: 'document',
populate: { path: 'sub document'}
})
答案 2 :(得分:0)
正如JohnnyHK所说,您需要使用字段名称来填充。
如果您的帖子数组为空,请检查用户集合是否具有帖子ID的帖子数组。
以下是我可以使其工作的步骤:
1-)将2条这样的帖子插入帖子集合。
{
"_id" : ObjectId("5dc6cd65067a61191839ff38"),
"name" : "name 3",
"description" : "description 3",
"date" : ISODate("2019-11-09T17:29:57.249+03:00")
}
{
"_id" : ObjectId("5dc6cd5d067a61191839ff37"),
"name" : "name 2",
"description" : "description 2",
"date" : ISODate("2019-11-09T17:29:49.070+03:00")
}
2-)插入了一个具有这2个帖子ID的用户。
{
"_id" : ObjectId("5dc6cf5b67da8a40cc519866"),
"posts" : [
ObjectId("5dc6cd5d067a61191839ff37"),
ObjectId("5dc6cd65067a61191839ff38")
],
"name" : "user 1",
"date" : ISODate("2019-11-09T17:38:19.807+03:00")
}
3-)并使用以下途径在用户中获取帖子。
router.get("/allUsers", async (req, res) => {
const allUsers = await User.find().populate("posts");
res.send(allUsers);
});
结果是这样的:
[
{
"posts": [
{
"_id": "5dc6cd5d067a61191839ff37",
"name": "name 2",
"description": "description 2",
"date": "2019-11-09T14:29:49.070Z",
"__v": 0
},
{
"_id": "5dc6cd65067a61191839ff38",
"name": "name 3",
"description": "description 3",
"date": "2019-11-09T14:29:57.249Z",
"__v": 0
}
],
"_id": "5dc6cf5b67da8a40cc519866",
"name": "user 1",
"date": "2019-11-09T14:38:19.807Z",
"__v": 0
}
]
顺便,您的用户和后期建模并不理想。每次用户发布时,用户文档都必须更新。
我将使用以下架构(父引用)。
用户模型(我删除了posts字段,并添加了虚拟功能)
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
name: { type: String, required: true },
date: { type: Date, default: Date.now }
}, {
toJSON: { virtuals: true },
toObject: { virtuals: true }
});
userSchema.virtual("posts", {
ref: "Post",
foreignField: "userId",
localField: "_id"
})
const User = mongoose.model("User", userSchema);
module.exports = User;
发布模型(我添加了userId)
const mongoose = require("mongoose");
const postSchema = new mongoose.Schema({
name: { type: String, required: true },
description: { type: String, required: true },
date: { type: Date, default: Date.now },
userId: { type: mongoose.Schema.ObjectId, ref: "User" }
});
const Post = mongoose.model("Post", postSchema);
module.exports = Post;
通过这种方式,用户发布时用户文档不会受到影响。而且,您可以使用相同的/ allUsers路由来获取包含其帖子的用户。