Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoBD Learning (3) insert and query data

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Root@adela-vm:/opt/mongodb_simple# numactl-- interleave=all bin/mongod-f conf/mongod.conf

About to fork child process, waiting until server is ready for connections.

Forked process: 4904

Child process started successfully, parent exiting

Root@adela-vm:/opt/mongodb_simple#. / bin/mongo 127.0.0.1:12345

MongoDB shell version: 2.6.9

Connecting to: 127.0.0.1:12345/test

> show dbs / / shows several databases

Admin (empty)

Local 0.078GB

> use adela / / switch to adela database without prior definition

Switched to db adela

> show dbs / / adela database does not add content, so it is not realistic

Admin (empty)

Local 0.078GB

>

The tables in RDBMS correspond to the collection collections in mongoDB, and the inserts are in json format

Db. Collection name (table) .insert (json format)

Db.adela_collection.insert ({"x": 1})

Show all collections

> show collections

Adela_collection

System.indexes

Insert data

> db.adela_collection.insert ({ypur2})

WriteResult ({"nInserted": 1})

> db.adela_collection.find ()

{"_ id": ObjectId ("5514f09641daf13c28f7bdb7"), "x": 1}

{"_ id": ObjectId ("5514f12b41daf13c28f7bdb8"), "y": 2}

_ id is automatically assigned by the system and can be customized, but _ id cannot be repeated.

> db.adela_collection.insert ({aVera 1, recorder 1})

WriteResult ({"nInserted": 1})

> db.adela_collection.insert ({aVera 2, recorder 1})

WriteResult ({

"nInserted": 0

"writeError": {

"code": 11000

"errmsg": "insertDocument:: caused by:: 11000 E11000 duplicate key error index: adela.adela_collection.$_id_ dup key: {: 1.0}"

}

})

Query adela_collection full table

> db.adela_collection.find () / / No parameters are added

Query data with x = 1

> db.adela_collection.find ({XRV 1})

{"_ id": ObjectId ("5514f09641daf13c28f7bdb7"), "x": 1}

{"_ id": ObjectId ("5514f1d241daf13c28f7bdb9"), "x": 1}

Insert a batch of data in a loop

> for (iTun1teri db.adela_collection.find ()

{"_ id": ObjectId ("5514f09641daf13c28f7bdb7"), "x": 1}

{"_ id": ObjectId ("5514f12b41daf13c28f7bdb8"), "y": 2}

{"_ id": ObjectId ("5514f1d241daf13c28f7bdb9"), "x": 1}

{"_ id": 1, "a": 1}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe54"), "z": 1}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe55"), "z": 2}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe56"), "z": 3}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe57"), "z": 4}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe58"), "z": 5}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe59"), "z": 6}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe5a"), "z": 7}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe5b"), "z": 8}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe5c"), "z": 9}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe5d"), "z": 10}

Count count

> db.adela_collection.find () .count ()

fourteen

How many pieces of data did skip skip?

Limit limits how many pieces of data to return

Sort uses sorting

> db.adela_collection.find ()

{"_ id": ObjectId ("5514f09641daf13c28f7bdb7"), "x": 1}

{"_ id": ObjectId ("5514f12b41daf13c28f7bdb8"), "y": 2}

{"_ id": ObjectId ("5514f1d241daf13c28f7bdb9"), "x": 1}

{"_ id": 1, "a": 1}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe54"), "z": 1}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe55"), "z": 2}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe56"), "z": 3}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe57"), "z": 4}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe58"), "z": 5}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe59"), "z": 6}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe5a"), "z": 7}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe5b"), "z": 8}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe5c"), "z": 9}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe5d"), "z": 10}

> db.adela_collection.find () .skip (4)

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe54"), "z": 1}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe55"), "z": 2}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe56"), "z": 3}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe57"), "z": 4}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe58"), "z": 5}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe59"), "z": 6}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe5a"), "z": 7}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe5b"), "z": 8}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe5c"), "z": 9}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe5d"), "z": 10}

> db.adela_collection.find () .skip (4) .limit (5)

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe54"), "z": 1}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe55"), "z": 2}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe56"), "z": 3}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe57"), "z": 4}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe58"), "z": 5}

> db.adela_collection.find () .skip (4) .limit (5) .sort ({zan1})

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe54"), "z": 1}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe55"), "z": 2}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe56"), "z": 3}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe57"), "z": 4}

{"_ id": ObjectId ("5514f5eca1b17c0bb936fe58"), "z": 5}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report