Mongodb 在Mongo中填充后,如何查询包含相同用户名的所有文档
我正在尝试获取所有与用户名字段匹配并具有相同值的文档 例如,如果用户名是Mabel,则返回填充后用户名与名称Mabel匹配的每个文档 项目模型:Mongodb 在Mongo中填充后,如何查询包含相同用户名的所有文档,mongodb,mongoose,aggregation-framework,Mongodb,Mongoose,Aggregation Framework,我正在尝试获取所有与用户名字段匹配并具有相同值的文档 例如,如果用户名是Mabel,则返回填充后用户名与名称Mabel匹配的每个文档 项目模型: { "_id": "5e641a033ecd5c172827eba6", "title": "My Post", "description": "First post", "userId": "5e431ba109c629018d664d89", "createdAt": "2020-03-07T22:02:43
{
"_id": "5e641a033ecd5c172827eba6",
"title": "My Post",
"description": "First post",
"userId": "5e431ba109c629018d664d89",
"createdAt": "2020-03-07T22:02:43.758Z",
"__v": 0
}
填充后:(在项目模型内部填充用户模型)
我试过什么:
const projects = await Projects.find().populate({
path: "userId",
match: { username: { $in: username } }
});
但最终从所有其他用户名获取所有文档
我需要什么
现在,Mabel创建了三个项目。这三个项目属于同一个userId.username。如何从该用户名查询所有这些文档?我猜您是在试图让用户了解他/她的项目 使用聚合框架可以很容易地做到这一点 我们首先将用户与用户名匹配,然后使用聚合来获取他/她的项目
const Users=require(“../models/user”)//根据您的用户模型更改此路径
const Projects=require(“../models/project”)//根据项目模型更改此路径
路由器.get(“/users”),异步(req,res)=>{
让username=“Mabel”;
const result=await Users.aggregate([
{
$match:{
用户名:username
}
},
{
$lookup:{
from:“projects”,//必须是集合的物理名称
localField:“\u id”,
foreignField:“用户ID”,
as:“项目”
}
}
]);
res.send(结果);
});
样本文件:
db={
"users": [
{
"_id": "5e6a293637a3d101b488278d",
"username": "Mabel",
"role": "user",
"__v": 0
},
{
"_id": "5e6a294537a3d101b488278e",
"username": "Tom",
"role": "user",
"__v": 0
},
{
"_id": "5e6a294f37a3d101b488278f",
"username": "Bob",
"role": "user",
"__v": 0
}
],
"projects": [
{
"_id": "5e6a298537a3d101b4882790",
"title": "My Post",
"description": "First post",
"userId": "5e6a293637a3d101b488278d",
"__v": 0
},
{
"_id": "5e6a29a237a3d101b4882791",
"title": "My Post 2",
"description": "Second post",
"userId": "5e6a293637a3d101b488278d",
"__v": 0
},
{
"_id": "5e6a29bd37a3d101b4882792",
"title": "Tom post",
"description": "Tom post description",
"userId": "5e6a294537a3d101b488278e",
"__v": 0
},
{
"_id": "5e6a2a0837a3d101b4882793",
"title": "Bob post",
"description": "Bob post description",
"userId": "5e6a294f37a3d101b488278f",
"__v": 0
}
]
}
输出:
[
{
"__v": 0,
"_id": "5e6a293637a3d101b488278d",
"projects": [
{
"__v": 0,
"_id": "5e6a298537a3d101b4882790",
"description": "First post",
"title": "My Post",
"userId": "5e6a293637a3d101b488278d"
},
{
"__v": 0,
"_id": "5e6a29a237a3d101b4882791",
"description": "Second post",
"title": "My Post 2",
"userId": "5e6a293637a3d101b488278d"
}
],
"role": "user",
"username": "Mabel"
}
]
第二种选择:
如果要使用填充而不是聚合,则需要设置。因为我们在用户内部没有项目引用
因此,为了设置虚拟填充,我们在用户模式中进行以下更改:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const UserSchema = new Schema(
{
username: String,
role: String
},
{
toJSON: { virtuals: true } //don't forget this option
}
);
UserSchema.virtual("projects", {
ref: "Project", // Project here must match mongoose.model("Project", ProjectSchema);
foreignField: "userId",
localField: "_id"
});
module.exports = mongoose.model("User", UserSchema);
现在我们可以像这样获得用户和他/她的项目:
router.get(“/users”),异步(req,res)=>{
让username=“Mabel”;
const result=await Users.find({username}).populate(“项目”);
res.send(结果);
});
Wow。。谢谢你,先生,让我检查一下!让你知道你好,先生,我意识到整个用户字段都在项目内部填充。现在我看不到任何项目:(.是否有任何更改,以便我可以筛选该用户的所有项目?我正在设置路径/:username==>以便每次点击该路径时,所有项目都从该用户名返回。如果我问得太多,请原谅。@MabelMoscoso您的意思是您得到的结果与答案中的输出不同吗?是的。我很抱歉。您的方法正是我真正需要的。我只是,现在只有用户出现,我无法过滤项目。这意味着这是正确的方法,但缺少的是也显示项目信息,以便我现在可以使用find()中的条件
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const UserSchema = new Schema(
{
username: String,
role: String
},
{
toJSON: { virtuals: true } //don't forget this option
}
);
UserSchema.virtual("projects", {
ref: "Project", // Project here must match mongoose.model("Project", ProjectSchema);
foreignField: "userId",
localField: "_id"
});
module.exports = mongoose.model("User", UserSchema);