Updating/upsert/insert to a nested collection in Meteor &
Updating/upsert/insert to a nested collection in Meteor & MongoDB
我有这个集合和架构:
Words = new Mongo.Collection("words");
WordsSchema = new SimpleSchema({
date: {
type: String
},
owner: {
type: String
},
entries: {
type: [Entry]
}
});
Entry = new SimpleSchema({
post: {
type: String
},
elapsed_time: {
type: Number
},
createdAt: {
type: Date
}
});
Words.attachSchema( WordsSchema );
我只是想通过UPSERT(或用UPSERT=true更新)将条目添加到这个集合中,如下所示:
Words.update({
date: today,
owner: Meteor.userId()
}, {
$push: {
entries: { post: post, elapsed_time: elapsed_time, createdAt: new Date() }
}
}, {
upsert: true
});
即,如果存在日期=今天(存储为"YYYY-MM-DD")且所有者=用户ID的对象,则它只添加特定条目。
但我做错了什么,因为我得到了错误:"条目是必需的"。
非常感谢任何帮助或想法!谢谢
我不确定这是怎么回事。但我猜简单模式不支持类型:[Entry]。因此,在这里您可以尝试这两种方法来实现相同的功能。1.对两种模式都进行Marge。
WordsSchema = new SimpleSchema({
date: {
type: String
},
owner: {
type: String
},
entries: {
type: Array
}
"entries.$": {
type: Object
},
"entries.$.post: {
type: String
},
"entries.$.verified": {
type: Boolean
},
"entries.$.verified": {
type: Boolean
}
});
2.通过使用$,您可以分离两种模式
WordsSchema = new SimpleSchema({
date: {
type: String
},
owner: {
type: String
},
entries: {
type: Array
}
"entries.$": {
type: Entry
}
});
Entry = new SimpleSchema({
post: {
type: String
},
elapsed_time: {
type: Number
},
createdAt: {
type: Date
}
});
相关文章:
- Node.js's Buffer.writeFloatBE in Javascript
- Meteor如何接收HTTP请求
- Meteor-将选定窗体中的对象添加到集合中
- Meteor上的启动页面
- Setting default onclick behavior for <img> tag in gene
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- Meteor-添加用户自定义字段的方法不起作用
- Meteor忘记了密码的实现
- 链接所有<a>Meteor
- 在Meteor项目中安装并包含npm模块后出错
- Lucene Search in Alfresco
- Meteor JS中代码的重复使用部分
- Meteor Router数据函数被调用两次
- 基于localStorage的Meteor激活模板
- Updating/upsert/insert to a nested collection in Meteor &
- 如何访问 mongos db.system.js in meteor
- new ReactiveDict() in meteor 1.2
- Meteor - layoutTemplate in RouteController.extend in package
- this.next() error in Meteor Iron Router
- Rooms/Channels and userId in Meteor.js