Meteor collection2 类型对象
Meteor collection2 type Object
我正在尝试创建一个字段 modifiedBy,类型为:对象(对 Meteor 用户)。
我看到您可以为自定义对象设置 blackbox: true,但是如果我想设置为特定对象,请说组(集合)字段 modifiedBy 是登录用户,任何 pointers/help 都很好赞赏。
谢谢
据我所知,你有两个选择:
- 用
type: String
存储用户 ID
- 按照您的建议对其进行反规范化
按照您的建议对其进行反规范化
要对其进行非规范化,您可以在您的模式中执行类似的操作:
...
modifiedBy: {
type: object
}
'modifiedBy._id': {
type: String,
autoValue: function () {
return Meteor.userId()
}
}
'modifiedBy.username': {
type: String,
autoValue: function () {
return Meteor.user().username
}
}
...
正如您所指出的,您希望在这些属性发生变化时对其进行更新:
服务器端
Meteor.users.find().observe({
changed: function (newDoc) {
var updateThese = SomeCollection.find({'modifiedBy.username': {$eq: newDoc._id}})
updateThese.forEach () {
SomeCollection.update(updateThis._id, {$set: {name: newDoc.profile.name}})
}
}
})
用 type: String
在那里存储用户 ID
我建议存储用户 ID。它更清洁,但性能不如其他解决方案。方法如下:
...
modifiedBy: {
type: String
}
...
您也可以轻松编写 Custom Validator
for this. Now retrieving the Users is a bit more complicated. You could use a transform function 来获取用户对象。
SomeCollection = new Mongo.Collection('SomeCollection', {
transform: function (doc) {
doc.modifiedBy = Meteor.users.findOne(doc.modifiedBy)
return doc
}
})
但有一个问题:"Transforms are not applied for the callbacks of observeChanges or to cursors returned from publish functions."
这意味着要以响应方式检索文档,您必须编写一个抽象:
getSome = (function getSomeClosure (query) {
var allDep = new Tacker.Dependency
var allChanged = allDep.changed.bind(allDep)
SomeCollection.find(query).observe({
added: allChanged,
changed: allChanged,
removed: allChanged
})
return function getSome () {
allDep.depend()
return SomeCollection.find(query).fetch()
}
})
我正在尝试创建一个字段 modifiedBy,类型为:对象(对 Meteor 用户)。
我看到您可以为自定义对象设置 blackbox: true,但是如果我想设置为特定对象,请说组(集合)字段 modifiedBy 是登录用户,任何 pointers/help 都很好赞赏。
谢谢
据我所知,你有两个选择:
- 用
type: String
存储用户 ID
- 按照您的建议对其进行反规范化
按照您的建议对其进行反规范化
要对其进行非规范化,您可以在您的模式中执行类似的操作:
...
modifiedBy: {
type: object
}
'modifiedBy._id': {
type: String,
autoValue: function () {
return Meteor.userId()
}
}
'modifiedBy.username': {
type: String,
autoValue: function () {
return Meteor.user().username
}
}
...
正如您所指出的,您希望在这些属性发生变化时对其进行更新:
服务器端
Meteor.users.find().observe({
changed: function (newDoc) {
var updateThese = SomeCollection.find({'modifiedBy.username': {$eq: newDoc._id}})
updateThese.forEach () {
SomeCollection.update(updateThis._id, {$set: {name: newDoc.profile.name}})
}
}
})
用 type: String
在那里存储用户 ID
我建议存储用户 ID。它更清洁,但性能不如其他解决方案。方法如下:
...
modifiedBy: {
type: String
}
...
您也可以轻松编写 Custom Validator
for this. Now retrieving the Users is a bit more complicated. You could use a transform function 来获取用户对象。
SomeCollection = new Mongo.Collection('SomeCollection', {
transform: function (doc) {
doc.modifiedBy = Meteor.users.findOne(doc.modifiedBy)
return doc
}
})
但有一个问题:"Transforms are not applied for the callbacks of observeChanges or to cursors returned from publish functions."
这意味着要以响应方式检索文档,您必须编写一个抽象:
getSome = (function getSomeClosure (query) {
var allDep = new Tacker.Dependency
var allChanged = allDep.changed.bind(allDep)
SomeCollection.find(query).observe({
added: allChanged,
changed: allChanged,
removed: allChanged
})
return function getSome () {
allDep.depend()
return SomeCollection.find(query).fetch()
}
})