0

现在我已经使用这个填充工作

{
    "_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."
    }
  ]
}
4

1 回答 1

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
    }
  ]
}
于 2013-08-14T17:10:10.647 回答