Sample Code for common mongodb select operations in php, mongodbselect
We have mentioned mongodb installation, configuration, cluster, and php insertion and update. For more information, see mongodb.The following describes common mongodb select operations.
Test data:Copy codeThe Code is as follows:{"_ Id": 1, "title":
://bj.xiaozh u.com/search-duanzufang-p{}-0/'. Format (str) in a range (1, 4)] def insert_house_info (url_s): for URL _a in url_s: # get page Data Wb_data = Requests.get (url_a) # with lxml parsing engine, parse data soup = BeautifulSoup (Wb_data.text, ' lxml ') prices = Soup.select (' span.result_price ') titles = Soup.select (' #page_list > UL > li > Div.result_btm_con.lodgeunitname > div > A > span ') urls = soup.select (' #page_list > UL Gt li > Div.result_btm_con.lodgeunitname ') for price
Database backup –mongodump
Back up all local MongoDB databases:
Copy Code code as follows:
# mongodump-h 127.0.0.1--port 27017-o/root/db/alldb
To back up a remote specified database:
Copy Code code as follows:
# mongodump-h 192.168.1.233--port 27018-d yourdb-o/root/db/yourdb
More Mongodump Detailed
Database Restore-mongorestore
Restore all databases to MongoDB
MongoDB is a document-type database, one of the most important members of the NoSQL family, and the following code encapsulates the basic operations of MongoDB.
Mongodbconfig.java
Package Com.posoftframework.mongodb;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import java.util.Enumeration;
Import Java.util.HashMap;
Import java.util.Hashtable;
Import java.util.List;
data in the array: by the $ character. If you want to match two attributes when querying in an array, you will need to use the $elemmatch method, assuming that only {"name": "Google", "Address": USA} to find, Will select all the elements named Google or address for the USA. After locating this element, select it by $ to change it.Person.update ({"Name": "Jobs",company:{$elemMatch: {"name": "Google", "Address": USA}}}, {$set: {"Company.$.workingtime": 4}},function () {})[3], delete the data spec
MongoDB want to integrate PHP, need to install MONGO extension, this is relatively simple, now say MongoDB Phpapi and usageLet's look at a simple example:
PHP code
1.
2. $m = new Mongo (); This is the default connection to the local 27017 port, of course, you can also connect remote host such as 192.168.0.4:27017, if the port is 27017, the port can be omitted
3.
4. $db = $m-> comedy; Select the comedy
Recent projects need to use MongoDB access files, here is a simple PHP example:
Public Function run () {Initialize Gridfs$m = new Mongoclient (); Connection$db = $m->selectdb ("Excel");Dump ($m), exit;$collection = $db->testexcel;$grid = $db->getgridfs (); Get Gridfs ObjectGridfs has three ways of storing filesThe first type of direct storage file$id = $grid->storefile (dirname (__file__). /emptyaction.class.php ");echo $id;Get files$file = $grid->fi
Copy codeThe Code is as follows: // Use the default port 27017 to connect to the local machine. Of course, you can also connect to a remote host such as 192.168.0.4: 27017. If the port is 27017, the port can be omitted.
$ M = new Mongo ();
// Select the comedy database. If the database is not created before, you can also use $ m-> selectDB ("comedy ");
$ Db = $ m-> comedy;
// Select the collection set in comedy, which is equivalent to the table in RDBMS. You can also use
$ Collection = $ db-> co
Copy codeThe Code is as follows:// Use the default port 27017 to connect to the local machine. Of course, you can also connect to a remote host such as 192.168.0.4: 27017. If the port is 27017, the port can be omitted.$ M = new Mongo ();// Select the comedy database. If the database is not created before, you can also use $ m-> selectDB ("comedy ");$ Db = $ m-> comedy;// Select the collection set in comedy, which is equivalent to the table in RDBMS. You can also use$ Collection = $ db-> collecti
Copy Code code as follows:
This is the default connection to the local 27017 port, of course, you can also connect remote host such as 192.168.0.4:27017, if the port is 27017, the port can be omitted
$m = new Mongo ();
Select the comedy database, if the database is not previously created automatically, you can also use $m->selectdb ("comedy");
$db = $m->comedy;
Select the collection set within the comedy, which is equivalent to the table in the RDBMS, also-you can use
$collection = $d
syntax is a bit like an object-oriented query language that almost implements most of the functionality of a relational database single-table query, and also supports indexing of data. MONGO can also solve the query efficiency of massive data, according to official documents, when the amount of data reached more than 50GB data, MONGO database access speed is more than MySQL10 times. BSON is the abbreviation for Binay JSON, BSON has the following three features: 1 Light weight 2 boast platform
PHP Connection MongoDB Example:
This is the default connection to the local 27017 port, of course, you can also connect remote host such as 192.168.0.4:27017, if the port is 27017, the port can be omitted
$m = new Mongo ();
Select the comedy database, if the database is not previously created automatically, you can also use $m->selectdb ("comedy");
$db = $m->comedy;
Select the collection set within the comedy, which is equivalent to the table i
libraries and tables created in the actions above
Insert
Var MongoClient = require(‘mongodb‘).MongoClient;
Var DB_CONN_STR = ‘mongodb://localhost:27017/wilsondb1‘;
Var insertData = function(db, callback) {
//Connect to the table
Var collection = db.collection(‘tb2‘);
/ / Insert data
Var data = [{"name":‘wilson001‘,"age":21},{"name":‘wilson002‘,"ag
name. Remove (condition); Stored ProceduresTo create a stored procedure:Db.system.js.save ({_id: "Stored procedure ID", value:function (parameter) { --logical body; return returns; }}); Call a stored procedureDb.eval ("Stored procedure ID ()"); All stored procedures are kept in db.system.jsMongoDB Basic operation is said so much, basic enough, in-depth study we have to see the API ^_^!Nodejs manipulating MongoDBInstall Mo
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.