php连接mongodb进行增删改查代码

代码语言:php

所属分类:其他

代码描述:php连接mongodb进行增删改查代码,注意此代码在php7.2上运行有效

代码标签: 进行 删改

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
//插入数据

$bulk = new MongoDB\Driver\BulkWrite;
$document = ['_id' => new MongoDB\BSON\ObjectID, 'name' => 'BFW'];

$_id = $bulk->insert($document);

var_dump($_id);

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $manager->executeBulkWrite('test.bfw', $bulk, $writeConcern);
//读取数据

$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");

// 插入数据
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1, 'name' => 'baidu', 'url' => 'http://www.baidu.com']);
$bulk->insert(['x' => 2, 'name' => 'Google', 'url' => 'http://www.google.com']);
$bulk->insert(['x' => 3, 'name' => 'taobao', 'url' => 'http://www.taobao.com']);
$manager->executeBulkWrite('test.sites', $bulk);

$filter = ['x' => ['$gt' => 1]];
$options = [
    'projection' => ['_id' => 0],
    'sort' => ['x�.........完整代码请登录后点击上方下载按钮下载查看

网友评论0