Get the App
SLTechnology News&Howtos  ›  Database  › 

Installation and CRUD operation of Mongodb

Shulou Source: shulou.com Published: 2022-06-01 12:32:03 09月22日 Update

What is Mongodb ?

Mongo DB is an open source, non-relational database (NoSql) with a flexible document model that allows you to develop smoothly. For Internet applications with large data volume, high concurrency and weak transactions, MongoDB can handle it freely. MongoDB's built-in horizontal scaling mechanism provides data processing capabilities ranging from millions to billions, which can fully meet the data storage needs of Web2.0 and mobile Internet. Its out-of-the-box features also greatly reduce the O & M costs of small and medium-sized websites.

install MongoDB

Go to mongodb official website http://www.mongodb.org/You can find the YUM source here and install two packages corresponding to the version, one for the server and one for the client. The package name is as follows:

mongo-10gen-2.4.12-mongodb_1.x86_64.rpm

mongo-10gen-server-2.4.12-mongodb_1.x86_64.rpm

Add official yum source:

vim /etc/yum.repos.d/monogdb.repo

[mongodb]name=MongoDB Repositorybaseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/gpgcheck=0enabled=1

yum install mongo-10gen.x86_64 mongo-10gen-server.x86_64 -y

Create mongodb data folder

mkdir /mongodb/data/

chown -R mongod.mongod /mongodb/data/

Modify the configuration file/etc/mongod.conf to specify the data directory

dbpath=/mongodb/data

Start Mongodb

service mongod start

CRUD operation of Mongodb

[root@server1 ~]# mongo

MongoDB shell version: 2.4.5

connecting to: test

> show dbs //Show database

local0.078125GB

testdb0.203125GB

> use testdb //Use the database without creating it in advance.

switched to db testdb

> db.testmcoll.insert({Name: "Jerry"}) //Specify collection insert data

> db.testmcoll.insert({Name:"Haiman"})

> show collections //Show collections in the library

system.indexes

testmcoll

> db.testmcoll.find() //Find data in a collection

{ "_id" : ObjectId("549fcaa56e8223a06e8b1f52"), "Name" : "Jerry" }

{ "_id" : ObjectId("549fcadc6e8223a06e8b1f53"), "Name" : "Haiman" }

> db.testmcoll.stats() //Output status information for the collection

{

"ns" : "testdb.testmcoll",

"count" : 2,

"size" : 80,

"avgObjSize" : 40,

"storageSize" : 4096,

"numExtents" : 1,

"nindexes" : 1,

"lastExtentSize" : 4096,

"paddingFactor" : 1,

"systemFlags" : 1,

"userFlags" : 0,

"totalIndexSize" : 8176,

"indexSizes" : {

"_id_" : 8176

},

"ok" : 1

}

>

> db.testmcoll.drop() //Delete collection

true

Multivalued insertion and bulk insertion

db.users.insert( { name:"Tom", age:23, status:"S", groups:[ "News","concert" ] } )

for(i=1;im.txt

Summary complete!

Tags: Data documents fields operations users age database conditions Internet files types interconnection query smooth free free two number small and medium-sized Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Huawei macOS OPPO Reno MariaDB