不带任何参数的remove()方法删除集合中的所有文档.
$this->db->$collection->remove();
Run Code Online (Sandbox Code Playgroud)
但是如何使用安全模式删除所有文档?
应该删除的第一个参数是什么?传递数组('safe'=> true)作为第一个参数不会删除所有文档,因为它被视为具有键'safe'的过滤器.
$this->db->$collection->remove(array('safe' => true));
Run Code Online (Sandbox Code Playgroud) MongoDB + PHP:如何查询存储在mongodb对象中的文档大小?在撰写本文时(最近从4MB增加),文档限制为16MB.
如何使用ObjectId查询文档的大小?
我需要这样做一个查询:
db.subscribers.find({LISTID: 59, {$or: [{LANG: {$in: ['English', 'Spanish']} },
{LANG: {$nin: ['Russian']} }]} });
Run Code Online (Sandbox Code Playgroud)
这应该是来自listid 59的所有订阅者,他们懂英语或西班牙语,或者(并且)不懂俄语.可以有任何可以有多个值的字段,而不是语言.看起来这个查询在mongodb控制台中有效.在这种情况下,一个字段可以处于几种条件.我有相同的字段不会在几个条件,也许它可能是这样的:
$collection->find(array('LISTID' => $listId,
array('$or' = array(LANG => array('$in' => array('English','Spanish')),
/*some other condition united with OR*/
)));
Run Code Online (Sandbox Code Playgroud)
如何在PHP中进行此查询?
我得到一个错误
Array
(
[errmsg] => exception: the $unwind field path must be specified as a string
[code] => 15981
[ok] => 0
)
Run Code Online (Sandbox Code Playgroud)
虽然我使用以下查询给定的嵌入式文档(我想从我给定的表结构记录中的rate_number的总和)
global $DB, $mongo;
$theObjId = new MongoId($post_id);
$collection = $mongo->getCollection('mongo_hw_posts');
$rt_sum = $collection->aggregate(
array('$unwind'=>$rate),
array('$group'=>
array(
'_id' => $theObjId
),
array(
'rate_number'=>array('$sum' =>$rate.'rate_number')
))
);
Run Code Online (Sandbox Code Playgroud)
表结构
{
"_id": ObjectId("51ff3b38636e3b9803000001"),
"class_id": NumberInt(2986),
"created_by": NumberInt(1758),
"created_datetime": NumberInt(1375681336),
"deleted": NumberInt(0),
"learn": {
"0": {
"user_id": NumberInt(0),
"learn_date": NumberInt(0)
}
},
"parent_id": "0",
"post_text": "2%20C",
"post_type": "text_comment",
"rate": {
"0": …Run Code Online (Sandbox Code Playgroud)