猫鼬-使用条件更新或创建子文档

时间:2018-10-24 15:02:02

标签: mongodb mongoose insert-update subdocument

我正在尝试更新或创建一个子文档,该文档的父级条件和子级条件。

我的模式

“我的用户”模式包含一个帖子投票列表,其定义如下:

<Style x:Key="AccentedDialogSquareButton" TargetType="{x:Type Button}">
    ...
</Style>

当用户对帖子进行投票时,如果他是第一次投票,我想填充他的const UserSchema = new Schema({ _id ... votes: [{ postID: { type: String }, voteType: { type: String }, }], } 子文档,如果以前已经投票,则想更新votes的现有投票。

示例数据

voteType

我的尝试

变量User { _id: "11de484d-a41c-438c-8286-92eed096f550", ... votes: [ { postID: "ff3bb466-8be4-4879-8500-e0d407e87cda", voteType: "upVote" } ] } userIDpostID已经过测试。

voteType

我的结果

User.update({ "_id": userID, "votes.postID": postID }, { "$set": { "votes.$.voteType": voteType } }, { upsert: true }, function (err, doc) { console.log(doc); } ) 未填充。

0 个答案:

没有答案
相关问题