猫鼬填充不填充
Mongoose populate does not populate
我的 MongoDB 数据库中有两个集合:users 和 components.
用户的主键是_id,也就是Number。它被组件文档的 created_by 和 updated_by 字段引用。
除了 populate 之外,一切正常,我不明白为什么。它保留值而不是相应的用户文档。我也尝试使用 ObjectIds 而不是 Numbers 但没有任何改变。
我们将不胜感激。
我在上面列出了模型和路由来源。
组件和用户模型
models/user.js
import mongoose from 'mongoose';
const userSchema = new mongoose.Schema({
_id: Number,
firstName: String,
lastName: String,
email: String,
password: String,
});
const User = mongoose.model('User', userSchema);
export default User;
models/component.js
import mongoose from 'mongoose';
const componentSchema = new mongoose.Schema({
name: String,
category: String,
package: String,
box: Number,
cell: Number,
quantity: Number,
note: String,
link: String,
created_by: {
type: Number,
ref: 'User'
},
created_on: Date,
updated_by: {
type: Number,
ref: 'User'
},
updated_on: Date
});
const Component = mongoose.model('Component', componentSchema);
export default Component;
models/index.js
import Component from './component';
import User from './user';
const models = { Component, User };
export default models;
中间件
import models from './models';
app.use((req, res, next) => {
req.context = { models };
next();
});
组件路由
import { Router } from "express";
const router = Router();
...
var getComponents = async (req, res) => {
const query = req.context.models.Component.find().populate('users');
try {
const results = await query;
return res.status(200).send({ results: results });
} catch (err) {
return res.status(400).send({ error: err });
}
}
...
router.get('/', getComponents)
export default router;
我也试过使用 exec
var getComponents = (req, res) =>
req.context.models.Component.find().populate('users').exec().then(components => res.status(200).send({ results: components })).catch(err => res.status(400).send({ error: err }));
但我得到了相同的结果。
您只需要在填充路径中使用现有的字段名称,如 .populate('created_by')
这是测试:
我首先用这个请求正文创建了一个用户:
{
"_id": 111,
"firstName": "John",
"lastname": "River",
"email": "abc@def.net",
"password": "123123"
}
然后我创建了两个组件:
{
"name": "component 1",
"quantity": 1,
"created_by": 111
}
和
{
"name": "component 2",
"quantity": 2,
"created_by": 111
}
并使用此代码:
const result = await Component.find().populate("created_by");
结果是:
[
{
"_id": "5de7a89e9039c738b4008d41",
"name": "component 1",
"quantity": 1,
//other fields
"created_by": {
"_id": 111,
"firstName": "John",
"email": "abc@def.net",
"password": "123123",
"__v": 0
},
"__v": 0
},
{
"_id": "5de7a8aa9039c738b4008d42",
"name": "component 2",
"quantity": 2,
//other fields
"created_by": {
"_id": 111,
"firstName": "John",
"email": "abc@def.net",
"password": "123123",
"__v": 0
},
"__v": 0
}
]
我相信会是:
created_by: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
然后,这样查询,
Component.find().populate('created_by');
你可以这样做。
created_by: { type:Schema.Types.ObjectId, ref: 'User' }
并查询
Component.find().populate('created_by');
我的 MongoDB 数据库中有两个集合:users 和 components.
用户的主键是_id,也就是Number。它被组件文档的 created_by 和 updated_by 字段引用。
除了 populate 之外,一切正常,我不明白为什么。它保留值而不是相应的用户文档。我也尝试使用 ObjectIds 而不是 Numbers 但没有任何改变。
我们将不胜感激。
我在上面列出了模型和路由来源。
组件和用户模型
models/user.js
import mongoose from 'mongoose';
const userSchema = new mongoose.Schema({
_id: Number,
firstName: String,
lastName: String,
email: String,
password: String,
});
const User = mongoose.model('User', userSchema);
export default User;
models/component.js
import mongoose from 'mongoose';
const componentSchema = new mongoose.Schema({
name: String,
category: String,
package: String,
box: Number,
cell: Number,
quantity: Number,
note: String,
link: String,
created_by: {
type: Number,
ref: 'User'
},
created_on: Date,
updated_by: {
type: Number,
ref: 'User'
},
updated_on: Date
});
const Component = mongoose.model('Component', componentSchema);
export default Component;
models/index.js
import Component from './component';
import User from './user';
const models = { Component, User };
export default models;
中间件
import models from './models';
app.use((req, res, next) => {
req.context = { models };
next();
});
组件路由
import { Router } from "express";
const router = Router();
...
var getComponents = async (req, res) => {
const query = req.context.models.Component.find().populate('users');
try {
const results = await query;
return res.status(200).send({ results: results });
} catch (err) {
return res.status(400).send({ error: err });
}
}
...
router.get('/', getComponents)
export default router;
我也试过使用 exec
var getComponents = (req, res) =>
req.context.models.Component.find().populate('users').exec().then(components => res.status(200).send({ results: components })).catch(err => res.status(400).send({ error: err }));
但我得到了相同的结果。
您只需要在填充路径中使用现有的字段名称,如 .populate('created_by')
这是测试:
我首先用这个请求正文创建了一个用户:
{
"_id": 111,
"firstName": "John",
"lastname": "River",
"email": "abc@def.net",
"password": "123123"
}
然后我创建了两个组件:
{
"name": "component 1",
"quantity": 1,
"created_by": 111
}
和
{
"name": "component 2",
"quantity": 2,
"created_by": 111
}
并使用此代码:
const result = await Component.find().populate("created_by");
结果是:
[
{
"_id": "5de7a89e9039c738b4008d41",
"name": "component 1",
"quantity": 1,
//other fields
"created_by": {
"_id": 111,
"firstName": "John",
"email": "abc@def.net",
"password": "123123",
"__v": 0
},
"__v": 0
},
{
"_id": "5de7a8aa9039c738b4008d42",
"name": "component 2",
"quantity": 2,
//other fields
"created_by": {
"_id": 111,
"firstName": "John",
"email": "abc@def.net",
"password": "123123",
"__v": 0
},
"__v": 0
}
]
我相信会是:
created_by: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
然后,这样查询,
Component.find().populate('created_by');
你可以这样做。
created_by: { type:Schema.Types.ObjectId, ref: 'User' }
并查询
Component.find().populate('created_by');