0
私はこのようなモデルとコレクションを持っています。別のモデルからサブ文書を検索する方法
user.jsの
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
name: String,
email: String, //convert to contacts with type Object
username: String,
password: String,
createdAt: {
type: Date,
default: Date.now
},
updatedAt: {
type: Date,
default: Date.now
}
});
const User = mongoose.model('user', UserSchema);
module.exports = User;
サーバコレクション
{
"_id" : ObjectId("591d3736e562d87e5f7a4435"),
"name" : "test.snapzio.com",
"description" : "test",
"hostname" : "hostname",
"ipaddress" : "192.168.1.1",
"status" : "good",
"updatedAt" : ISODate("2017-05-18T05:55:02.920+0000"),
"createdAt" : ISODate("2017-05-18T05:55:02.920+0000"),
"users" : [
{
"id" : "591d2fc1f484ce774c1ca58d",
},
{
"id" : "591d2fc1f484ce774c1ca58d",
},
{
"id" : "591d2fdbf484ce774c1ca58e",
}
],
"projects" : [
ObjectId("591d3079f484ce774c1ca590")
],
"__v" : NumberInt(0)
}
私は、レンダリング後、サーバー文書内のレコードに一致するユーザーを取得したいです。これは私がしたことです
router.get('/add_project/:id', ensureAuthenticated, (req, res, next) => {
Server.findById(req.params.id, (err, server) => {
User.find({ _id: server.users.id }, (err, users) => {
console.log(users);//undefined
});
});
});
サーバー文書のレコードと一致するすべてのユーザーを取得したいと考えています。スキーマを変更しないことを検討してください。