MongoDB - 帮我创建一个PHP查询

时间:2011-08-30 11:13:32

标签: php mongodb database

我有以下数据库条目:

{
   "_id": ObjectId("4d2339f9772c4bae5403dd47"),
   "uname": "Eamorr",
   "posts": {
     "0": {
       "comment": "asdf",
       "fromUname": "Anonymous",
       "time": 1294154233,
       "UTC": "2011-01-04T15: 17: 13+00: 00",
       "ip": "134.226.1.229",
       "id": "974vi95ljn5aa22ykd21"
    },
     "1": {
       "comment": "wddddd",
       "fromUname": "Anonymous",
       "time": 1294154360,
       "UTC": "2011-01-04T15: 19: 20+00: 00",
       "ip": "134.226.1.229",
       "id": "519fy1sj6f8mc4ws8v1y"
    },
     "2": {
       "comment": "asdf2",
       "fromUname": "Anonymous",
       "time": 1294162680,
       "UTC": "2011-01-04T17: 38: 00+00: 00",
       "ip": "86.40.63.120",
       "id": "ho27be183q3kqxi2w618"
    },
     "3": {
       "comment": "asdf3",
       "fromUname": "Anonymous",
       "time": 1294162759,
       "UTC": "2011-01-04T17: 39: 19+00: 00",
       "ip": "86.44.19.49",
       "id": "bib1184hb8e90f67zhac"
    },
     "4": {
       "comment": "Eamonn",
       "fromUname": "Anonymous",
       "time": 1294162884,
       "UTC": "2011-01-04T17: 41: 24+00: 00",
       "ip": "86.44.19.49",
       "id": "rbq18o0r73mdfc8a023n"
    },
     "5": {
       "comment": "Eamonn2",
       "fromUname": "Anonymous",
       "time": 1294162967,
       "UTC": "2011-01-04T17: 42: 47+00: 00",
       "ip": "86.44.19.49",
       "id": "p1210t688d0g5c0t97jx"
    },
     "6": {
       "comment": "asdf",
       "fromUname": "Anonymous",
       "time": 1294235472,
       "UTC": "2011-01-05T13: 51: 12+00: 00",
       "ip": "134.226.1.229",
       "id": "y128u1m57bw1n711gr8o"
    },
     "7": {
       "comment": "asdf",
       "fromUname": "Anonymous",
       "time": 1294235557,
       "UTC": "2011-01-05T13: 52: 37+00: 00",
       "ip": "134.226.1.229",
       "id": "5g1xc51mm057p39c5l1p"
    },
     "8": {
       "comment": "asdf",
       "fromUname": "Anonymous",
       "time": 1294235617,
       "UTC": "2011-01-05T13: 53: 37+00: 00",
       "ip": "134.226.1.229",
       "id": "m35g1ug0220g82lq8z31"
    },
     "9": {
       "comment": "asdf",
       "fromUname": "Anonymous",
       "time": 1294240505,
       "UTC": "2011-01-05T15: 15: 05+00: 00",
       "ip": "134.226.1.229",
       "id": "i9ngh02d8ifg464088x2"
    },
     "10": {
       "comment": "boards.ie",
       "fromUname": "Anonymous",
       "time": 1294240542,
       "UTC": "2011-01-05T15: 15: 42+00: 00",
       "ip": "134.226.1.229",
       "id": "vcu1ks7ev1swaeu6z2r4"
    },
     "11": {
       "comment": "Hello",
       "fromUname": "Eamorr",
       "time": 1295618860,
       "UTC": "2011-01-21T14: 07: 40+00: 00",
       "ip": "134.226.252.160",
       "id": "00kk05gp258clzk15mxs"
    },
     "12": {
       "comment": "asdf",
       "fromUname": "Eamorr",
       "time": 1295624407,
       "UTC": "2011-01-21T15: 40: 07+00: 00",
       "ip": "212.129.64.223",
       "id": "6knwupaws7641a7fvsq2"
    },
     "13": {
       "comment": "asdf",
       "fromUname": "Eamorr",
       "time": 1295624465,
       "UTC": "2011-01-21T15: 41: 05+00: 00",
       "ip": "212.129.64.223",
       "id": "rl4k3i710xkwr5njq36l"
    },
     "14": {
       "comment": "Hi",
       "fromUname": "Eamorr",
       "time": 1295635332,
       "UTC": "2011-01-21T18: 42: 12+00: 00",
       "ip": "212.129.64.223",
       "id": "w2q11m6kaqdw9p8k44v4"
    },
     "15": {
       "comment": "Hello!!!",
       "fromUname": "Eamorr2",
       "time": 1296082365,
       "UTC": "2011-01-26T22: 52: 45+00: 00",
       "ip": "86.44.21.90",
       "id": "001cwy768eik620rka91"
    },
     "16": {
       "comment": "Hello",
       "fromUname": "Eamorr",
       "time": 1296280251,
       "UTC": "2011-01-29T05: 50: 51+00: 00",
       "ip": "86.44.20.113",
       "id": "0wfe8r9490lf5r1s11kv"
    },
     "17": {
       "comment": "Hello",
       "fromUname": "Eamorr",
       "time": 1298632583,
       "UTC": "2011-02-25T11: 16: 23+00: 00",
       "ip": "86.44.49.74",
       "id": "71k5cn8zrmsudy77513g"
    },
     "18": {
       "comment": "just wanted to say hi",
       "fromUname": "Anonymous",
       "time": 1299175499,
       "UTC": "2011-03-03T18: 04: 59+00: 00",
       "ip": "71.235.146.42",
       "id": "u2ejx9d812vk6147me6m"
    },
     "19": {
       "comment": "Hello",
       "fromUname": "Anonymous",
       "time": 1299238987,
       "UTC": "2011-03-04T11: 43: 07+00: 00",
       "ip": "212.129.64.223",
       "id": "ibl84549kz220va052qm"
    },
     "20": {
       "comment": "Hello",
       "fromUname": "Anonymous",
       "time": 1299239150,
       "UTC": "2011-03-04T11: 45: 50+00: 00",
       "ip": "212.129.64.223",
       "id": "503hl41wmb73756h1oe0"
    },
     "21": {
       "comment": "Hello",
       "fromUname": "Anonymous",
       "time": 1299239230,
       "UTC": "2011-03-04T11: 47: 10+00: 00",
       "ip": "212.129.64.223",
       "id": "8894rpqow2i38b777cky"
    },
     "22": {
       "comment": "Hello",
       "fromUname": "Anonymous",
       "time": 1299239341,
       "UTC": "2011-03-04T11: 49: 01+00: 00",
       "ip": "212.129.64.223",
       "id": "37885igze3q81wcf8ko7"
    },
     "23": {
       "comment": "Hello",
       "fromUname": "Anonymous",
       "time": 1299239409,
       "UTC": "2011-03-04T11: 50: 09+00: 00",
       "ip": "212.129.64.223",
       "id": "3p2095098p96315u7632"
    },
     "24": {
       "comment": "Hello",
       "fromUname": "Anonymous",
       "time": 1299239561,
       "UTC": "2011-03-04T11: 52: 41+00: 00",
       "ip": "212.129.64.223",
       "id": "j8d79nwb65u4cl8808l8"
    },
     "25": {
       "comment": "Hello",
       "fromUname": "Anonymous",
       "time": 1299239727,
       "UTC": "2011-03-04T11: 55: 27+00: 00",
       "ip": "212.129.64.223",
       "id": "202ts1046jkuysd78509"
    },
     "26": {
       "comment": "Hello",
       "fromUname": "Anonymous",
       "time": 1299239793,
       "UTC": "2011-03-04T11: 56: 33+00: 00",
       "ip": "212.129.64.223",
       "id": "6ye47y3tr9syt20k5kgp"
    },
     "27": {
       "comment": "Hello",
       "fromUname": "Anonymous",
       "time": 1299239830,
       "UTC": "2011-03-04T11: 57: 10+00: 00",
       "ip": "212.129.64.223",
       "id": "55g039yzp4013bk4aqtr"
    },
     "28": {
       "comment": "Hello",
       "fromUname": "Anonymous",
       "time": 1299240076,
       "UTC": "2011-03-04T12: 01: 16+00: 00",
       "ip": "212.129.64.223",
       "id": "8w7sy170dku4y9o06d1g"
    },
     "29": {
       "comment": "Hello",
       "fromUname": "Anonymous",
       "time": 1299240113,
       "UTC": "2011-03-04T12: 01: 53+00: 00",
       "ip": "212.129.64.223",
       "id": "1noki588r7796v5mti2f"
    },
     "30": {
       "comment": "Hello",
       "fromUname": "Anonymous",
       "time": 1299240152,
       "UTC": "2011-03-04T12: 02: 32+00: 00",
       "ip": "212.129.64.223",
       "id": "l175d3aew064feaw9126"
    },
     "31": {
       "comment": "Hello",
       "fromUname": "Anonymous",
       "time": 1299240245,
       "UTC": "2011-03-04T12: 04: 05+00: 00",
       "ip": "212.129.64.223",
       "id": "5rpz421351p7qrv5o7rk"
    },
     "32": {
       "comment": "Hello2",
       "fromUname": "Anonymous",
       "time": 1299240280,
       "UTC": "2011-03-04T12: 04: 40+00: 00",
       "ip": "212.129.64.223",
       "id": "n031287n17eo14v9tuda"
    },
     "33": {
       "comment": "Hello",
       "fromUname": "Anonymous",
       "time": 1299240365,
       "UTC": "2011-03-04T12: 06: 05+00: 00",
       "ip": "212.129.64.223",
       "id": "609773uq8rsv5vuw4s0k"
    },
     "34": {
       "comment": "Hello2",
       "fromUname": "Anonymous",
       "time": 1299240406,
       "UTC": "2011-03-04T12: 06: 46+00: 00",
       "ip": "212.129.64.223",
       "id": "57w4v64ekl76np304d9k"
    },
     "35": {
       "comment": "Hello3",
       "fromUname": "Anonymous",
       "time": 1299240441,
       "UTC": "2011-03-04T12: 07: 21+00: 00",
       "ip": "212.129.64.223",
       "id": "7v6agdd2xdgm2zyya5pb"
    },
     "36": {
       "comment": "Hello",
       "fromUname": "Eamorr",
       "time": 1299241138,
       "UTC": "2011-03-04T12: 18: 58+00: 00",
       "ip": "212.129.64.223",
       "id": "1qp2a2837x31791xxg0s"
    },
     "37": {
       "comment": "asdf",
       "fromUname": "Eamorr",
       "time": 1299241395,
       "UTC": "2011-03-04T12: 23: 15+00: 00",
       "ip": "212.129.64.223",
       "id": "x7app929kw9l4p1bk977"
    },
     "38": {
       "comment": "lkjh",
       "fromUname": "Anonymous",
       "time": 1299241477,
       "UTC": "2011-03-04T12: 24: 37+00: 00",
       "ip": "212.129.64.223",
       "id": "ea9ws389971pmt6waee8"
    },
     "39": {
       "comment": "Hello",
       "fromUname": "Eamorr",
       "time": 1299244791,
       "UTC": "2011-03-04T13: 19: 51+00: 00",
       "ip": "212.129.64.223",
       "id": "bfk9tm2gt6z254739z08"
    },
     "40": {
       "comment": "asdf",
       "fromUname": "Anonymous",
       "time": 1299249252,
       "UTC": "2011-03-04T14: 34: 12+00: 00",
       "ip": "212.129.64.223",
       "id": "ufaf48c1qd1e0n07s8m5"
    },
     "41": {
       "comment": "asdf",
       "fromUname": "Anonymous",
       "time": 1299250483,
       "UTC": "2011-03-04T14: 54: 43+00: 00",
       "ip": "212.129.64.223",
       "id": "3k65kne9h0pk410xzc0p"
    },
     "42": {
       "comment": "asdf",
       "fromUname": "Anonymous",
       "time": 1299251270,
       "UTC": "2011-03-04T15: 07: 50+00: 00",
       "ip": "212.129.64.223",
       "id": "t7740r0fcby6gowlw9xk"
    },
     "43": {
       "comment": "Hi, how's it going?",
       "fromUname": "Eamorr",
       "time": 1299683127,
       "UTC": "2011-03-09T15: 05: 27+00: 00",
       "ip": "212.129.64.223",
       "id": "9l864xc2agxsdjo4257y"
    },
     "44": {
       "comment": "3hvdn",
       "fromUname": "Anonymous",
       "time": 1299683730,
       "UTC": "2011-03-09T15: 15: 30+00: 00",
       "ip": "212.129.64.223",
       "id": "4frfsf12xwz1to9po0w3"
    },
     "45": {
       "comment": "Hi there",
       "fromUname": "Anonymous",
       "time": 1299684889,
       "UTC": "2011-03-09T15: 34: 49+00: 00",
       "ip": "212.129.64.223",
       "id": "hw4i9mk906n1qvg63o2d"
    },
     "46": {
       "comment": "asdf",
       "fromUname": "Anonymous",
       "time": 1313371240,
       "UTC": "2011-08-15T02: 20: 40+01: 00",
       "ip": "212.129.66.130",
       "id": "0f4o199394hzqyv6by89"
    },
     "47": {
       "comment": "asdf",
       "fromUname": "Anonymous",
       "time": 1313430246,
       "UTC": "2011-08-15T18: 44: 06+01: 00",
       "ip": "212.129.66.131",
       "id": "73xbuqwauhls17cb3dc3"
    },
     "48": {
       "comment": "vvvvvvvv",
       "fromUname": "Anonymous",
       "time": 1313430944,
       "UTC": "2011-08-15T18: 55: 44+01: 00",
       "ip": "212.129.66.131",
       "id": "44dw9427805j93g51u2h"
    },
     "49": {
       "comment": "vvvvvvvv",
       "fromUname": "Anonymous",
       "time": 1313431402,
       "UTC": "2011-08-15T19: 03: 22+01: 00",
       "ip": "212.129.66.131",
       "id": "lo1n5i2qpxa562o606hd"
    },
     "50": {
       "comment": "vvvvvvvv",
       "fromUname": "Anonymous",
       "time": 1313431865,
       "UTC": "2011-08-15T19: 11: 05+01: 00",
       "ip": "212.129.66.131",
       "id": "fu01r3z2vk12ar0avk65"
    },
     "51": {
       "comment": "xxxxccccc",
       "fromUname": "Anonymous",
       "time": 1313432538,
       "UTC": "2011-08-15T19: 22: 18+01: 00",
       "ip": "212.129.66.131",
       "id": "1fk5u60nkok8hyuzd0e6"
    },
     "52": {
       "comment": "xxxxccccc",
       "fromUname": "Anonymous",
       "time": 1313432587,
       "UTC": "2011-08-15T19: 23: 07+01: 00",
       "ip": "212.129.66.131",
       "id": "x20adl504ur7652r7x7b"
    },
     "53": {
       "comment": "'cccccccccccvvvvv",
       "fromUname": "Anonymous",
       "time": 1313432791,
       "UTC": "2011-08-15T19: 26: 31+01: 00",
       "ip": "212.129.66.131",
       "id": "nwk0m8lq7cc3s9748cve"
    },
     "54": {
       "comment": "aaaassss",
       "fromUname": "Anonymous",
       "time": 1313432849,
       "UTC": "2011-08-15T19: 27: 29+01: 00",
       "ip": "212.129.66.131",
       "id": "99fj5rl2ne7hzr78c691"
    },
     "55": {
       "comment": "asdffffffff",
       "fromUname": "Anonymous",
       "time": 1313477562,
       "UTC": "2011-08-16T07: 52: 42+01: 00",
       "ip": "212.129.66.131",
       "id": "y81iemo7t501dlwo3e52"
    },
     "56": {
       "comment": "Charlie",
       "fromUname": "Anonymous",
       "time": 1313525636,
       "UTC": "2011-08-16T21: 13: 56+01: 00",
       "ip": "212.129.66.130",
       "id": "k6bi9czy2g1941c7cid5"
    },
     "57": {
       "comment": "asdf",
       "fromUname": "Anonymous",
       "time": 1314036401,
       "UTC": "2011-08-22T19: 06: 41+01: 00",
       "ip": "212.129.66.131",
       "id": "zs4v1f7sr82f106p42gr"
    },
     "58": {
       "comment": "asdf2",
       "fromUname": "Anonymous",
       "time": 1314036666,
       "UTC": "2011-08-22T19: 11: 06+01: 00",
       "ip": "212.129.66.131",
       "id": "jg3y5xwilljq6hi7mpwd"
    },
     "59": {
       "comment": "qwerty",
       "fromUname": "Eamorr",
       "time": 1314550970,
       "UTC": "2011-08-28T18: 02: 50+01: 00",
       "ip": "212.129.66.131",
       "id": "uvoh7abs5u2kfogb28kr"
    },
     "60": {
       "comment": "qwerty",
       "fromUname": "Eamorr",
       "time": 1314551143,
       "UTC": "2011-08-28T18: 05: 43+01: 00",
       "ip": "212.129.66.131",
       "id": "u6eqi91y539mgv2ufxfw"
    },
     "61": {
       "comment": "qwerty2",
       "fromUname": "Eamorr",
       "time": 1314551158,
       "UTC": "2011-08-28T18: 05: 58+01: 00",
       "ip": "212.129.66.130",
       "id": "1dxz445s16qi17020y26"
    },
     "62": {
       "comment": "jacks",
       "fromUname": "Eamorr",
       "time": 1314633471,
       "UTC": "2011-08-29T16: 57: 51+01: 00",
       "ip": "212.129.66.131",
       "id": "3nkq2cxq2okb205z9ao6"
    }
  }
}

我正在尝试根据'id'删除条目。

因此,给定id“3nkq2cxq2okb205z9ao6”(恰好是最后一个条目,#62),如何更新此mongo集合以删除元素62?

我真的被困住了。我看到了类似的stackoverflow问题,但我真的需要一些特定的问题。

非常感谢任何见解。

非常感谢,

3 个答案:

答案 0 :(得分:1)

如果我们谈论的ID是MongoId(自动创建),那么您应该使用;

$collection->remove(array('id' => new MongoId('3nkq2cxq2okb205z9ao6')), true);

但如果id字段是你随机生成的东西,那么下面的代码就可以了;

$collection->remove(array('id' => '3nkq2cxq2okb205z9ao6'));

答案 1 :(得分:0)

在php.net上查看此评论

http://www.php.net/manual/en/mongocollection.remove.php#95418

并使用搜索!

答案 2 :(得分:0)

好的,这就是我设法做的事情(尽管并不理想):

$idx=0;
for($i=0;$i<sizeof($posts);$i++){
  $post=$posts[$i];
  if($post['id']==$id){
    $idx=$i;
    break;
  }
}
unset($posts[$idx]);
$collection->update(array('uname'=>$uname),array('$set'=>array('posts'=>$posts)));