MongoDB:$ inc可以增加$ addToSet中的值吗? [英] MongoDB: Can a $inc increment a value inside a $addToSet

查看:51
本文介绍了MongoDB:$ inc可以增加$ addToSet中的值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MongoDB相对较新,并且对更高级的ups有麻烦.我一直在谷歌搜索和阅读文档,但是很难确切知道我在寻找什么.基本上,我正在创建一个计数器,该计数器将存储多个域的数据.

I'm relatively new to MongoDB and having trouble with a more advanced upsert. I've been Googling and reading the documentation but having trouble knowing exactly what I'm looking for. Basically I'm creating a hit counter which will store data for multiple domains.

我的文档结构是:

{
    "domain": "example.com",
    "hitCount": 1,
    "urls": [
        {
            "url": "/the-url",
            "hitCount": 1,
            "hits": [
                {
                    "date": "2011-10-30T04:50:01.090Z",
                    "IP": "123.123.123.123"
                }
            ]
        }
    ]
}

到目前为止,我的upsert代码是:

My upsert code so far is:

{
    $set: {"domain": "example.com"},
    $inc: {"hitCount": 1},
    $addToSet: {"urls": {"url": "/the-url"} }
}

这个位工作得很好,但是正如您所看到的,它只是upsert的第一部分.我无法将其余数据插入"URL"中,例如增加"hitCount"并添加匹配的日期"和"IP".

This bits working great but as you can see its only the first part of the upsert. I'm having trouble inserting the rest of the data inside "urls" such as incrementing the "hitCount" and adding the "date" and "IP" of the hit.

我想知道这种文档结构是否可以一次发表?我开始认为我需要执行多个查询才能实现这一目标?

I was wondering if this document structure is possible in one upsert? I'm starting to think I need to do multiple queries to achieve this?

推荐答案

您必须执行多个查询.

这篇关于MongoDB:$ inc可以增加$ addToSet中的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆