mongodb - 如何使用 10gen C# 驱动程序更新内部属性?

标签 mongodb mongodb-.net-driver 10gen-csharp-driver

我有这样的结构:

public class User
{
    public ObjectId Id { get; set; }
    public Location Location { get; set; }
    public DateTime LastAround {get;set;}
}

public class Location
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

我已经尝试了一些方法,但我想更新用户的位置以及他们上次出现的时间。

尝试过这个:

userHelper.Collection.Update(
    Query.EQ("_id", userId),
    Update.SetWrapped<Location>("Location", new Location { Latitude = latitude, Longitude = longitude }).Set("LastAround", DateTime.UtcNow));

还有这个:

userHelper.Collection.Update(
    Query.EQ("_id", userId),
    Update.Set("Location.Latitude", latitude)
        .Set("Location.Longitude", longitude)
        .Set("LastAround", DateTime.UtcNow));

没有任何作用...我该怎么做?

4/17 更新:

userHelper.Collection.Update(
                Query.EQ("_id", new ObjectId(userId)),
                Update
                    .SetWrapped<Location>("Location", new Location { Longitude = longitude, Latitude = latitude })
                    .Set("LastAround", DateTime.UtcNow)
            );

在对 lng 和 lat 值进行查询时,它们似乎非常重要。我正在做一个 geonear 查询并得到一个奇怪的越界错误。如果您以错误的顺序更新,它将把纬度放在第一位,然后您会收到错误。

最佳答案

您的两个原始 Update 语句都应该有效。我编写了一个小示例程序来演示。

执行此插入语句后:

var userId = ObjectId.GenerateNewId();
var user = new User
{
    Id = userId,
    Location = new Location { Latitude = 1.0, Longitude = 2.0 },
    LastAround = new DateTime(2012, 4, 14, 0, 0, 0, DateTimeKind.Utc)
};
collection.Insert(user);

该文档在 mongo shell 中如下所示:

> db.test.find()
{ "_id" : ObjectId("4f8c5d33e447ad34b8c7ac84"), "Location" : { "Latitude" : 1, "Longitude" : 2 }, "LastAround" : ISODate("2012-04-14T00:00:00Z") }
>

执行第一种形式的Update语句后:

collection.Update(
    Query.EQ("_id", userId),
    Update
        .SetWrapped<Location>("Location", new Location { Latitude = 3.0, Longitude = 4.0 })
        .Set("LastAround", new DateTime(2012, 4, 15, 0, 0, 0, DateTimeKind.Utc)));

文档如下所示:

> db.test.find()
{ "_id" : ObjectId("4f8c5d33e447ad34b8c7ac84"), "Location" : { "Latitude" : 3, "Longitude" : 4 }, "LastAround" : ISODate("2012-04-15T00:00:00Z") }
>

执行第二种形式的Update语句后:

collection.Update(
    Query.EQ("_id", userId),
    Update
        .Set("Location.Latitude", 5.0)
        .Set("Location.Longitude", 6.0)
        .Set("LastAround", new DateTime(2012, 4, 16, 0, 0, 0, DateTimeKind.Utc)));

文档如下所示:

> db.test.find()
{ "_id" : ObjectId("4f8c5d33e447ad34b8c7ac84"), "Location" : { "Latitude" : 5, "Longitude" : 6 }, "LastAround" : ISODate("2012-04-16T00:00:00Z") }
>

因此,两种形式的 Update 语句都有效。

完整的程序在这里:

http://www.pastie.org/3799469

关于mongodb - 如何使用 10gen C# 驱动程序更新内部属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10158764/

相关文章:

node.js - 如何在 NodeJS 中保存和检索 MongoDB 中的 pdf 文件

mongodb - 如何从 mongodb 中的远程服务器复制受密码保护的数据库?

c# - MongoDB C#驱动多字段查询

f# - MongoDB自定义序列化程序实现

mongodb - 如何使用 MongoDB 和 C# 驱动程序查询子文档集合

Java:如何将 HashMap 插入 MongoDB?

Ruby - 按时区对 MongoDB 记录进行排序?

mongodb - 在 mongodb 连接字符串中处理 @

c# - 将 mongodb 连接到 C#

c# - 反序列化错误 ASP.NET MongoDB