现在我已经使用此
进行填充{
"_id": {
"$oid": "520953dde4b00c03eeb39950"
},
"username": "jdoe",
"fname": "Jave",
"lname": "Doe",
"role": "2",
"pinned": [
"5208ed90e4b0b41ab826ac9f",
"5208edb2e4b0b41ab826aca1"
],
"apps": [
"5208ed90e4b0b41ab826ac9f",
"5208edb2e4b0b41ab826aca1",
"5208ee29e4b0b41ab826aca7"
]
}
然后引用类似于此
的应用程序架构{
"_id": {
"$oid": "5208edb2e4b0b41ab826aca1"
},
"name": "My Application",
"route": "myapp",
"icon": "http://placehold.it/80x80&text=[App]",
"rating": 4,
"description": "Bacon ipsum dolor sit amet nulla ham qui sint exercitation eiusmod commodo, chuck duis velit. Aute in reprehenderit, dolore aliqua non est magna in labore pig pork biltong."
}
我正在使用像这样的模式填充
var UsersSchema = Schema({
username: String,
fname: String,
lname: String,
role: String,
apps: [{type:ObjectId, ref: "applications"}]
}, { collection : 'user' });
var ApplicationsSchema = new mongoose.Schema({}, { collection : 'applications' });
var User = mongoose.model('user', UsersSchema);
var Applications = mongoose.model('applications', ApplicationsSchema);
并按此执行。
User.findById(id, items , function(err, doc){
callback(doc);
}).populate("apps");
所以这是我的问题 我如何或者可以像这样设置我的MongoDB
{
"_id": {
"$oid": "520953dde4b00c03eeb39950"
},
"username": "abritez",
"fname": "Jave",
**"pinned": true,**
"lname": "Doe",
"role": "2",
"apps": [
{id : "5208ed90e4b0b41ab826ac9f", pinned:true},
{id : "5208edb2e4b0b41ab826aca1", pinned:true},
{id : "5208ee29e4b0b41ab826aca7", pinned:false}
]
}
我应该如何设置我的架构并填充执行?或者这不可能吗?我希望它看起来像这样。
{
"_id": "520953dde4b00c03eeb39950",
"fname": "Jane",
"lname": "Doe",
"role": "2",
"apps": [
{
"pinned": true,
"_id": "5208ed90e4b0b41ab826ac9f",
"name": "App A",
"route": "appA",
"icon": "http://placehold.it/80x80&text=[App]",
"rating": 4,
"description": "Bacon ipsum dolor sit amet nulla ham qui sint exercitation eiusmod commodo, chuck duis velit. Aute in reprehenderit, dolore aliqua non est magna in labore pig pork biltong."
},
{
"_id": "5208edb2e4b0b41ab826aca1",
"pinned": true,
"name": "App B",
"route": "appB",
"icon": "http://placehold.it/80x80&text=[App]",
"rating": 4,
"description": "Bacon ipsum dolor sit amet nulla ham qui sint exercitation eiusmod commodo, chuck duis velit. Aute in reprehenderit, dolore aliqua non est magna in labore pig pork biltong."
},
{
"_id": "5208ee29e4b0b41ab826aca7",
"name": "App C",
"pinned": true,
"route": "appC",
"icon": "http://placehold.it/80x80&text=[App]",
"rating": 4,
"description": "Bacon ipsum dolor sit amet nulla ham qui sint exercitation eiusmod commodo, chuck duis velit. Aute in reprehenderit, dolore aliqua non est magna in labore pig pork biltong."
}
]
}
答案 0 :(得分:0)
通过使用点符号作为参数,以及对我使用的架构进行一些调整,结果变得非常简单。
User.find({}, function(err, doc){
callback(doc);
}).populate("apps.id");
我现在正在
{
"_id": "520bb759e4b0845c7706b271",
"fname": "Jane",
"lname": "Doe",
"role": "2",
"apps": [
{
"id": {
"_id": "5208ed90e4b0b41ab826ac9f",
"name": "App",
"route": "app",
"icon": "http://placehold.it/80x80&text=[App]",
"rating": 4,
"description": "Bacon ipsum dolor sit amet nulla ham qui sint exercitation eiusmod commodo, chuck duis velit. Aute in reprehenderit, dolore aliqua non est magna in labore pig pork biltong."
},
"pinned": true
}
]
}