Javascript 在Meteor&;中更新/向上插入/插入嵌套集合;蒙哥达
我有以下集合和模式:Javascript 在Meteor&;中更新/向上插入/插入嵌套集合;蒙哥达,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我有以下集合和模式: Words = new Mongo.Collection("words"); WordsSchema = new SimpleSchema({ date: { type: String }, owner: { type: String }, entries: { type: [Entry] } }); Entry = new SimpleSchema({ post: { type: String },
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
});
i、 e.如果存在日期为今天(存储为'YYYY-MM-DD')、所有者为userId的对象,则只添加特定条目
但我做错了什么,因为我得到了错误:“条目是必需的”
非常感谢您的任何帮助或想法!谢谢。我不确定这有什么问题。但我猜简单模式不支持类型:[Entry]。所以在这里,您可以尝试这两种方法来实现相同的功能。 1.Marge和schema
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
}
});
二,。通过使用$,您可以分离两个模式
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
}
});