1024programmer Java thinkphp cooperates with mongodb

thinkphp cooperates with mongodb

First add a database connection with mongodb in config.php

  1. 'DB_TYPE'=>'mongo',// Database type
  2. 'DB_HOST'=>'localhost',// Server address span>
  3. 'DB_NAME '=>'fzdb',// Database name
  4. 'DB_USER'=>'',// Username
  5. 'DB_PWD'=>'' ,// Password
  6. 'DB_PORT'=>27017,// port
  7. 'DB_PREFIX'=>'fz_',// Database table prefix span>
  8. 'DB_CHARSET '=>'utf8',// Database character set

Create the mongodb module in indexcontroller.class.php in the controller

  1. \Think\Build::buildModel('Home', 'Student' );

Then go to Model and modify this file

  1. <?php
  2. namespace Home\Model;
  3. use Think\Model\MongoModel;
  4. class StudentModel span>extends MongoModel {
  5. }

Query dataTechnology Sharing
Technology Sharing
mongodb database add information

  1. $m=D('student');
  2. //Add data information
  3. $d['sname']='Zhang Sanfeng';
  4. $m->add($d);

mongodb database query information

  1. $m=D('student');
  2. $rs=$m->select();
  3. dump($rs);

Fill in the field name and modify the data information according to the id. Use save

  1. / /Modify data information
  2. $d['_id']='559c983564f7676816000029 ';
  3. $d['sname']= 'Lian Shaorui';
  4. $m->save($d);

Delete specified information based on id

  1. //Delete the specified id
  2. $m->delete('559c984264f767d00b000029');

From Wiz Notes (Wiz)

thinkphp cooperates with mongodb

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/764998

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top