MongoDB:$inc 可以增加 $addToSet 中的值吗
我对 MongoDB 比较陌生,在更高级的 upsert 方面遇到了麻烦。我一直在谷歌搜索并阅读文档,但无法确切地知道我在寻找什么。基本上我正在创建一个点击计数器,它将存储多个域的数据。
我的文档结构是:
{
"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 代码是:
{
$set: {"domain": "example.com"},
$inc: {"hitCount": 1},
$addToSet: {"urls": {"url": "/the-url"} }
}
这一点工作得很好,但正如您所看到的,它只是 upsert 的第一部分。我在将其余数据插入“url”内时遇到问题,例如增加“hitCount”并添加点击的“日期”和“IP”。
我想知道这个文档结构是否可以在一次更新插入中实现?我开始认为我需要进行多个查询才能实现这一目标?
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.
My document structure is:
{
"domain": "example.com",
"hitCount": 1,
"urls": [
{
"url": "/the-url",
"hitCount": 1,
"hits": [
{
"date": "2011-10-30T04:50:01.090Z",
"IP": "123.123.123.123"
}
]
}
]
}
My upsert code so far is:
{
$set: {"domain": "example.com"},
$inc: {"hitCount": 1},
$addToSet: {"urls": {"url": "/the-url"} }
}
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?
您必须执行多个查询。
You must perform multiple queries.