【问题标题】:access subdocument in mongoDB访问 mongoDB 中的子文档
【发布时间】:2013-11-08 22:33:53
【问题描述】:

我在 mongoDB 中有一个集合用户,每个用户都有一个小部件文档,如下所示:

Widgets = [{
type: 'container',
size: 12,
offset: 0,
id: 'root',
children: [
    {
        type: 'widgetSearch',
        title: 'Recherche',
        offset: 0,
        size: 2,
        id: 'searchId',
        toolbar: {
            buttons: [ 'config', 'move', 'minimize', 'maximize', 'close' ]
        }
    },...etc

在文件 client.js 中我想访问 Widgets 数据。

我试试这个:

var user = Meteor.user();
var test = Meteor.users.find({_id: user._id});

console.log(test.widgets);

console.log(test[0].widgets);

我在这里做错了什么?

【问题讨论】:

  • 如果子文档名为 Widgets(大写 W),您可能需要输入 test.Widgets 来访问子文档

标签: javascript mongodb meteor subdocument


【解决方案1】:

默认情况下,Meteor 不会为用户帐户发布自定义字段。您需要自己发布每个所需的用户字段。

Meteor.publish(null, function() {
  return Meteor.users.find({_id: this.userId}, {fields: {widgets: 1, profile: 1, etc: 1 }});
});

【讨论】:

    猜你喜欢
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 2023-03-28
    相关资源
    最近更新 更多