Meteor 使用 MongoDB-collection 和已经存在的 objects 数组
Meteor use MongoDB-collection with array of objects that already exists
我制作了一个 python 脚本来读取一个文件并创建一个新的 mongoDB collection。
collection如下,当我在python中打印一份collection时:
{'_id': '5b5b0a55ca902423007413b9',
'employee': 'John Doe',
'schedule': [{'date': '08/11/2018', 'project': 'Drawing'},
...
{'date': '05/06/2018', 'project': 'Teaching'}
]
}
我用这段代码在 meteor 中做了一个 collection:
Planning = new Meteor.Collection("Planning");
Planning也是我在python中给collection取的名字。
现在,当我在 Meteor(服务器端)中 运行 这段代码时:
Meteor.methods({
getFullPlanning: function(){
var one = Planning.find({ employee: 'John Doe'});
console.log(one.employee);
}
});
这段代码记录未定义,但在我的 collection 中有一个同名的员工。我在 Meteor 中做错了什么?
但这确实有效:
Planning.find().count() // = 53 which is correct!
根据Meteor docs:
find
returns a cursor. It does not immediately access the database or return documents. Cursors provide fetch
to return all matching documents, map
and forEach
to iterate over all matching documents, and observe
and observeChanges
to register callbacks when the set of matching documents changes.
您的变量 one
是一个游标,而不是文档。如果您只查找单个文档,则可以调用 Planning.findOne({ employee: 'John Doe' })
它将 return 单个文档,如果未找到匹配项,则调用 undefined。您也可以调用 Planning.find({ employee: 'John Doe' }).fetch()
.
我制作了一个 python 脚本来读取一个文件并创建一个新的 mongoDB collection。 collection如下,当我在python中打印一份collection时:
{'_id': '5b5b0a55ca902423007413b9',
'employee': 'John Doe',
'schedule': [{'date': '08/11/2018', 'project': 'Drawing'},
...
{'date': '05/06/2018', 'project': 'Teaching'}
]
}
我用这段代码在 meteor 中做了一个 collection:
Planning = new Meteor.Collection("Planning");
Planning也是我在python中给collection取的名字。 现在,当我在 Meteor(服务器端)中 运行 这段代码时:
Meteor.methods({
getFullPlanning: function(){
var one = Planning.find({ employee: 'John Doe'});
console.log(one.employee);
}
});
这段代码记录未定义,但在我的 collection 中有一个同名的员工。我在 Meteor 中做错了什么?
但这确实有效:
Planning.find().count() // = 53 which is correct!
根据Meteor docs:
find
returns a cursor. It does not immediately access the database or return documents. Cursors providefetch
to return all matching documents,map
andforEach
to iterate over all matching documents, andobserve
andobserveChanges
to register callbacks when the set of matching documents changes.
您的变量 one
是一个游标,而不是文档。如果您只查找单个文档,则可以调用 Planning.findOne({ employee: 'John Doe' })
它将 return 单个文档,如果未找到匹配项,则调用 undefined。您也可以调用 Planning.find({ employee: 'John Doe' }).fetch()
.