Get the App
SLTechnology News&Howtos  ›  Database  › 

MongoBD Learning (3) insert and query data

Shulou Source: shulou.com Published: 2022-06-01 16:31:42 09月11日 Update

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}

Tags: Data database query content format parameter reality system allocation switching loop sorting restriction learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology Linux vpn MariaDB