使用Linq和MongoDB重新创建位置阵列操作员



在mongodb中我有一个可行的更新查询:

db.posts.update(
  { 
    "_id" : ObjectId("..."),
    "Comments.Reference" : 123 
  },
  { 
    $push : 
    { 
      "Comments.$.Notes": { Text: "Some description here" } 
    }
  });

这是在该数组中的匹配ID中找到带有匹配ID的邮政文档,并在该数组中的匹配引用中找到一个新对象。

但是,当我使用C#和驱动程序时,我想看看是否可以使用linq。

来创建它

我跌倒的地方正在创建一个更新查询,该查询转化为创建$ positional运算符。

IMongoUpdate update = Update<Post>.Push(t => t.Comments.First().Notes,
                                        BsonDocument.Parse("{ Text: "Test"}");

我将第一个()作为第一个猜测并将其进行编译,但是说它不能序列化它会引发错误。

是否可以重新创建此问题,还是我只需要恢复使用"注释。$。笔记"的字符串查询?

编辑:只是为了更新,这是有效的,但没有类型的安全性:

IMongoUpdate update = Update.Push("Comments.$.Notes",
                                  BsonDocument.Parse("{ Text: "Test"}");

添加了此功能:

https://jira.mongodb.org/browse/csharp-1046

直接从JIRA案件复制:

Builders<Entity>.Update.Set(x => x.MyArray[-1].Value, 10);

将产生

{ $set: { "MyArray.$.Value", 10 } }

不幸的是,目前似乎不支持它:(

https://jira.mongodb.org/browse/csharp-531

最新更新