如何从MongoDB的嵌套模式中提取特定模式?

7 浏览
0 Comments

如何从MongoDB的嵌套模式中提取特定模式?

我只需要从这个国家集合中获取州模式。

如何只获取州模式?

代码如下:

var mongoose = require('mongoose');
var iterationsSchema = mongoose.Schema({
    name: String,
    effectiveFrom: {type: Date, default: new Date()},
    active: {type: Boolean, default: true}
});
var citySchema = mongoose.Schema({
    cityName: String,
    iterations: [iterationsSchema],
    active: {type: Boolean, default: true}
});
var StateSchema = mongoose.Schema({
    stateName: String,
    iterations: [iterationsSchema],
    cities: [citySchema],
    active: {type: Boolean, default: true}
});
var CountrySchema = mongoose.Schema({
    countryName: String,
    iterations: [iterationsSchema],
    states: [StateSchema],
    active: {type: Boolean, default: true}
});
var Country = mongoose.model('Country', CountrySchema);
module.exports = Country;

0