doudou8081 2016-06-12 14:15
浏览 121
已采纳

如何通过搜索内部数组来检查记录是否存在

I have this record in DB

{
   "_id": ObjectId("5759ccbcc2b503980c000143"),
   "phone": "516-425-5878",
   "orders": [
     "1338475681",
     "1891805481",
     "1891805481" 
  ] 
}

Here is my code.

$collection = $mongo_db_obj->selectCollection('scrapers', 'leads');
$data = array(
    'phone' => '516-425-5878',
    "orders" => array('$in'=>1891805481)
);

$doc = $collection->findOne($data);

dump($doc);

I want to check if particular phone and and order id inside orders array exists or not. But my code is giving error.

I have tried this one too.

$data = array(
'phone' => '516-425-5878',
"orders" => array(1891805481)
 );

But it returns empty results.

How do I check that?

  • 写回答

1条回答 默认 最新

  • donglu6303 2016-06-12 15:02
    关注

    $in operator selects documents where field values matches values in an array.

    Please try executing following code snippet as a solution to above mentioned issue

    $conn = new MongoClient('localhost:27017');
    $db = $conn->selectDB("mydb");
    $collection=$db->mycoll;
    $data = array(
        'phone' => '516-425-5878',
        "orders" => array('$in'=>array('1891805481'))
    );
    $data=$collection->findOne($data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100