使用 $inc 和 $addToSet 更新 MongoDB [英] MongoDB upsert with $inc and $addToSet

查看:40
本文介绍了使用 $inc 和 $addToSet 更新 MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的示例代码:

<?php
$m = new Mongo();

$db = $m->selectDB('metrics');
$collection = new MongoCollection($db, 'counter');

$url = $_SERVER['REQUEST_URI'];
$ip = '192.168.1.1';
$user = 'testuser';
$today = date('Y-m-d');

//upsert 1st
$filter = array('url' => $url, 'date' => $today);
$data2 = array(
                '$inc' => array("pageview" => 1),
                '$addToSet' => array('visitors' => array('ip' => $ip))
);
$options = array("upsert" => true);
$collection->update( $filter, $data2, $options );


//update the pageview for unique ip after that
$filter1 = array( 'url' => $url, 'date' => $today, 'visitors' => array( '$elemMatch' => array( 'ip' => $ip ) ) );
$update1 = array( '$inc' => array( 'visitors.$.pageview' => 1 ) );
$collection->update( $filter1, $update1 );

?>

mongodb 中的数据:

Data in mongodb:

这是第一次查看页面时正确的.

This is correct when view the page for 1st time.

> db.counter.find();
{ "_id" : ObjectId("4fdaaf1176c5c9fca444ffd3"), "date" : "2012-06-14", "pageview" : 1, "url" : "/mongo/test.php", "visitors" : [ { "ip" : "192.168.1.1", "pageview" : 1 } ] }

刷新页面后,添加奇怪的数据:

After refresh the page, adding weird data:

> db.counter.find();
{ "_id" : ObjectId("4fdaaf1176c5c9fca444ffd3"), "date" : "2012-06-14", "pageview" : 2, "url" : "/mongo/test.php", "visitors" : [ { "ip" : "192.168.1.1", "pageview" : 2 }, { "ip" : "192.168.1.1" } ] }

如何更正查询并防止插入额外的 IP?谢谢.

How would I correct query and prevent the extra IP from being inserted? Thanks.

推荐答案

如果我没猜错,你粘贴的代码每次都是这样执行的?
你得到另一个 ip 条目的问题是 '$addToSet' =>array('visitors' => array('ip' => $ip))'$addToSet' =>array('visitors' => array('ip' => $ip)) 因为你的数组不包含一个只有 ip 的字段,它会插入另一个字段(你的字段还有一个 pageview 属性).您需要完全匹配.
不确定这个问题是否有一些指导方针,但我认为你应该使用这样的东西,作为文档结构:

If I got it right your pasted code is executed everytime in this way?
The problem that you get another ip entry is '$addToSet' => array('visitors' => array('ip' => $ip))'$addToSet' => array('visitors' => array('ip' => $ip)) as your array doesn't contain a field with only an ip it will insert another one(your field has additionally an pageview attribute). You would need to have an exact match.
Not sure if theres some guideline for this problem, but I think you should go with something like this, as document structure:

{_id:ObjectId(.....),..., 访客:{192.168.1.1: {pageview:1}}.所以你可以简单地运行这个更新命令(在 mongoshell 语法中),它进一步将两个更新合并为一个 :):

{_id:ObjectId(.....),..., visitors: { 192.168.1.1: {pageview:1}}. So you can simply run this update command (in mongoshell syntax), which further combines both updates into one :):

db.c.update({"url":"/test.php","date":"today"},{$inc : {"192.168.1.1" : {pageview:1},pageview: 1}},true) 更新您的文档.无论如何,您需要用其他字符替换这些点,否则它不起作用.

db.c.update({"url":"/test.php","date":"today"},{$inc : {"192.168.1.1" : {pageview:1}, pageview: 1}},true) to update your document. Anyway you need to replace those dots with some other character as it doesn't work otherwise.

这篇关于使用 $inc 和 $addToSet 更新 MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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