如何编写mongodb位置插入查询

时间:2015-09-12 12:10:22

标签: mongodb

我是mongodb的新手。我想为postalCode = 22222的所有文档中的地址字段插入位置[lon和lat]值[51.5625,7.9865]。我怎样才能做到这一点?请让我知道db query statement。

db.user.find({"address.postalCode":"222222"})

以上查询从我的数据库中提取10个文档。

我的用户集合中的示例地址对象

"address" : {
        "_id" : "7783-hsdh-7878-halkad-8989-lksdlk",
        "address" : "Department of State",
        "city" : "Washington",
        "stateProvince" : "Washington, DC",
        "postalCode" : "20521",
        "country" : "USA"
    },

所以,结果应该是

"address" : {
        "_id" : "7783-hsdh-7878-halkad-8989-lksdlk",
        "address" : "Department of State",
        "city" : "Washington",
        "stateProvince" : "Washington, DC",
        "postalCode" : "20521",
        "country" : "USA",
        "position" : [ 
           51.5625, 7.9865
         ]
    },

1 个答案:

答案 0 :(得分:0)

找到文档,修改值 markModified ,然后保存。

    private static async Task<StreamContent> CompressedContent(HttpContent content)
    {
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        using (System.IO.Compression.GZipStream gzip = new System.IO.Compression.GZipStream(ms,
                            System.IO.Compression.CompressionMode.Compress, true))
        {
            await content.CopyToAsync(gzip).OnThreadPool();
            await gzip.FlushAsync();
        }
        ms.Position = 0;

        StreamContent streamContent = new StreamContent(ms);
        streamContent.Headers.ContentType = content.Headers.ContentType;
        streamContent.Headers.Add("Content-Encoding", "gzip");
        return streamContent;
    }

    private async Task<string> GetResponseItemResult(string batchUri, MultipartContent cont, bool compressed = false)
    { 
        using (var httpClient = new HttpClient(new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }))
        using (var request = new HttpRequestMessage
        {
            RequestUri = new Uri(batchUri),
            Method = HttpMethod.Post
        })
        {
            if (compressed)
                request.Content = await CompressedContent(cont);
            else
                request.Content = cont;

            //Authentication stuff removed
            //...

            HttpResponseMessage responseMessage = await httpClient.SendAsync(request).OnThreadPool();

            //Error handling removed
            //...
            return await responseMessage.Content.ReadAsStringAsync();
        }

    }