Я хочу знать, есть ли способ получить вложенный документ, используя запрос mon goose, потому что я пробовал много взломов, но не нашел пути: (
Мой вопрос: как можно Я получаю документы о ценах или заполняю их?
UserQuest модель:
var mongoose = require("mongoose");
// var deepPopulate = require('mongoose-deep-populate')(mongoose);
var userQuestSchema = new mongoose.Schema({
status: { type: String, default: 'Unlocked' },
deadline: { type: Date, default: Date.now },
user: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
quest: { type: mongoose.Schema.Types.ObjectId, ref: "Quest" },
game: { type: mongoose.Schema.Types.ObjectId, ref: "Game" },
created_at: { type: Date, default: Date.now }
});
// userQuestSchema.plugin(deepPopulate);
// userQuestSchema.plugin(require('mongoose-autopopulate'));
module.exports = mongoose.model("Userquest", userQuestSchema);
QuestModel :
var mongoose = require("mongoose");
var deepPopulate = require('mongoose-deep-populate')(mongoose);
var questSchema = new mongoose.Schema({
title: String,
description: String,
kind: String,
type: String,
deadline: Number,
created_at: { type: Date, default: Date.now },
preconditions: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Precondition",
autopopulate: true
}
],
prices: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Price",
autopopulate: true
}
]
});
questSchema.plugin(require('mongoose-autopopulate'));
questSchema.plugin(deepPopulate);
module.exports = mongoose.model("Quest", questSchema);
Цена модель:
var mongoose = require("mongoose");
var priceSchema = new mongoose.Schema({
value: String,
category: String,
created_at: { type: Date, default: Date.now }
});
module.exports = mongoose.model("Price", priceSchema);
Я написал следующий код и получаю только квестовый документ:
Userquest.find({ user: req.user._id })
.populate({
path: 'quest',
model: 'Quest',
populate: {
path: 'quest.prices',
model: 'Quest',
}
}).exec(function (err, quests) {
if (err) {
console.log(err);
} else {
console.log(quests);
}
);
Результаты моего кода:
{
status: 'Unlocked',
_id: 5e4e9d7d90cf9932e491988a,
deadline: 2020 - 05 - 20T15: 53: 49.470Z,
user: 5d717de5559a3c0d48aed71e,
quest:
{
preconditions: [Array],
prices: [Array],
_id: 5e4ad26a4083dd45080c99cb,
kind: 'achievement',
type: 'totplayingtimes',
created_at: 2019 - 11 - 23T19: 32: 15.728Z,
title: 'Chuck,is that you ?',
description: '50 Games played',
deadline: 2160
},
created_at: 2020 - 02 - 20T14: 53: 49.496Z,
__v: 0
},
{
status: 'Unlocked',
_id: 5e4e9d7d90cf9932e491988b,
deadline: 2020 - 08 - 18T14: 53: 49.470Z,
user: 5d717de5559a3c0d48aed71e,
quest:
{
preconditions: [Array],
prices: [Array],
_id: 5e4ae1844083dd45080c99cd,
kind: 'achievement',
type: 'ranking',
created_at: 2019 - 11 - 23T19: 32: 15.728Z,
title: 'Big Boys club',
description: 'Reach Top 10',
deadline: 2160
},
created_at: 2020 - 02 - 20T14: 53: 49.509Z,
__v: 0
}