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

Basic operation of MongoDB

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

Share

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

MongoDB:

There are concepts of collections and documents

Collection = = tabl

Document = = rows in the database table

!! Create a database (bosenru)

> use bosenru

Switched to db bosenru

!! Insert a table (T1) into the bosenrui, and insert this field (x is the field, 1 is its value) into the table.

> db.ti.insert ({XRV 1})

WriteResult ({"nInserted": 1})

Check it out

> show tadabases

Admin (empty)

Bosenrui 0.078GB

Local 0.078GB

> use bosenru

Switched to db bosenru

> show tables

System.indexes

Ti

> db.t1.find ()

{"_ id": ObjectId ("5602402d575c3e0a6920d326"), "x": 1}

Is the only standard of it.

ObjectId ("5602402d575c3e0a6920d326")

> db.t1.insert ({XRV 2})

WriteResult ({"nInserted": 1})

> db.t1.find ()

{"_ id": ObjectId ("5602402d575c3e0a6920d326"), "x": 1}

{"_ id": ObjectId ("560240ea575c3e0a6920d327"), "x": 2}

!! Query only one data:

> db.t1.findOne ()

{"_ id": ObjectId ("56023de2575c3e0a6920d324"), "x": 1}

(the initials of One should be capitalized)

!! Delete the bosenrui library:

> db.dropDatabase ()

{"dropped": "bosenrui", "ok": 1}

(the initials of Database should be capitalized)

!! Use collections (tables), documents (row records)

Insert multiple pieces of data:

Mongodb can insert multiple pieces of data using json syntax

_ id: globally unique valu

> use bosenrui

Switched to db bosenrui

Specify (_ id) as 2

> db.t1.insert ({XRV 1, recorder ID 2})

WriteResult ({"nInserted": 1})

WriteResult ({"nInserted": 1})

> db.t1.find ()

{"_ id": 2, "x": 1}

> db.t1.findOne ()

{"_ id": 2, "x": 1}

If you insert the db.t1.insert ({XRX _ 1cr _ ID _ 2}), there will be an error, which is a primary key conflict. (do not specify the value of _ id, let the system assign it)

> db.t1.insert ({XRV 1, recorder ID 2})

WriteResult ({

"nInserted": 0

"writeError": {

"code": 11000

"errmsg": "insertDocument:: caused by:: 11000 E11000 duplicate key error index: bosenru.t1.$_id_ dup key: {: 2.0}"

!! Insert multiple pieces of data:

> for (iTun1teri db.t1.find ()

{"_ id": ObjectId ("560247edd5f225b12c0b67aa"), "x": 1}

{"_ id": ObjectId ("56024852d5f225b12c0b67ab"), "x": 1}

{"_ id": ObjectId ("56024852d5f225b12c0b67ac"), "x": 2}

{"_ id": ObjectId ("56024852d5f225b12c0b67ad"), "x": 3}

{"_ id": ObjectId ("56024852d5f225b12c0b67ae"), "x": 4}

Type "it" for more (you can turn pages with it)

> db.t1.find () .count ()

one hundred

There are 100 records in total

> db.t1.find () .skip (1) .limit (5) .sort ({x sort 1})

{"_ id": ObjectId ("56024852d5f225b12c0b67ab"), "x": 1}

{"_ id": ObjectId ("56024852d5f225b12c0b67ac"), "x": 2}

{"_ id": ObjectId ("56024852d5f225b12c0b67ad"), "x": 3}

{"_ id": ObjectId ("56024852d5f225b12c0b67ae"), "x": 4}

{"_ id": ObjectId ("56024852d5f225b12c0b67af"), "x": 5}

> db.t1.find () .skip (1) .limit (5) .sort ({XRAM1})

{"_ id": ObjectId ("56024852d5f225b12c0b680c"), "x": 98}

{"_ id": ObjectId ("56024852d5f225b12c0b680b"), "x": 97}

{"_ id": ObjectId ("56024852d5f225b12c0b680a"), "x": 96}

{"_ id": ObjectId ("56024852d5f225b12c0b6809"), "x": 95}

{"_ id": ObjectId ("56024852d5f225b12c0b6808"), "x": 94}

!! Skip 1 line and view 5 rows of records. According to this field, (1) is the order value (positive order), (- 1) (reverse order)

View the data of (_ id:2)

> db.t1.find ({'_ id':2})

{"_ id": 2, "x": 1}

! View (xRV? )

> db.t1.find ({'xdye VR2})

{"_ id": ObjectId ("56024852d5f225b12c0b67ac"), "x": 2}

> db.t1.find ({'xdye VR4})

{"_ id": ObjectId ("56024852d5f225b12c0b67ae"), "x": 4}

!! Update the data of (xposition 1) to (x lug 999)

> db.t1.update ({XRV 1}, {XRV 999})

WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})

Take a look at the data of (xpurl) again.

> db.t1.find ({'x _ blank _ 1})

{"_ id": ObjectId ("56024852d5f225b12c0b67ab"), "x": 1}

{"_ id": 2, "x": 1}

Take a look at the data again (xposition 999).

> db.t1.find ({'xdye rig 999})

{"_ id": ObjectId ("560247edd5f225b12c0b67aa"), "x": 999}

* if there is a need to repeat the new value in the collection, only the first one is updated

!! When multiple fields are updated, only part of the fields need to be updated

> db.t1.insert ({XRX 100Magi yRO 100J ZRO 100})

WriteResult ({"nInserted": 1})

> db.t1.find ()

{"_ id": ObjectId ("56024f44d5f225b12c0b680e"), "x": 100, "y": 100, "z": 100}

!! When update z is 100, y is 99

Db.t1.update ({ZRV 100}, {YRO 99})

WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})

Check it out:

> db.t1.find ()

{"_ id": ObjectId ("56024f44d5f225b12c0b680e"), "y": 99}

At this time, the value of (zmeme x) can not be found.

> db.t1.find ({'zipper 100})

> db.t1.find ({'xdye rig 100})

You can find the value of (ypur99).

> db.t1.find ({'yawning VL99})

{"_ id": ObjectId ("56024f44d5f225b12c0b680e"), "y": 99}

* this method is wrong

!! The right way:

Reinsert this piece of data

> db.t1.insert ({XRX 100Magi yRO 100J ZRO 100})

WriteResult ({"nInserted": 1})

> db.t1.update ({ZRV 100}, {$set: {YRV 99}})

WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1})

Check it out:

> db.t1.find ()

{"_ id": ObjectId ("56025482d5f225b12c0b6810"), "x": 100, "y": 99, "z": 100}

Db.t1.update ({zvv 100}, {$set: {Yvv 99}}), set is a partial update operator, updating existing fields, and fields that do not exist will remain as they are.

!! There is no data in the update collection:

> db.t1.find ({YVR 100})

>

There is no such data.

Update (yRO 100) to (yRO 99)

> db.t1.update ({YVR 100}, {YRO 999}, true)

WriteResult ({

"nMatched": 0

"nUpserted": 1

"nModified": 0

"_ id": ObjectId ("5602576b03e4970d981cb3bf")

Check it out:

> db.t1.find ({yvv 999})

{"_ id": ObjectId ("5602576b03e4970d981cb3bf"), "y": 999}

!! How to batch update:

Change (cRV 1) to (cRV 2)

Insert three pieces of data:

> db.t1.insert ({CRAV 1})

WriteResult ({"nInserted": 1})

> db.t1.insert ({CRAV 1})

WriteResult ({"nInserted": 1})

> db.t1.insert ({CRAV 1})

WriteResult ({"nInserted": 1})

Check it out:

> db.t1.find ()

{"_ id": ObjectId ("56025d10d5f225b12c0b6814"), "c": 1}

{"_ id": ObjectId ("56025d28d5f225b12c0b6815"), "c": 1}

{"_ id": ObjectId ("56025d29d5f225b12c0b6816"), "c": 1}

Batch updates:

> db.t1.update ({CRAV 1}, {$set: {CMV 2}}, false,true)

WriteResult ({"nMatched": 3, "nUpserted": 0, "nModified": 3})

Check it out:

> db.t1.find ()

{"_ id": ObjectId ("56025d10d5f225b12c0b6814"), "c": 2}

{"_ id": ObjectId ("56025d28d5f225b12c0b6815"), "c": 2}

{"_ id": ObjectId ("56025d29d5f225b12c0b6816"), "c": 2}

!! How to delete a value:

> db.t1.remove ({CRAV 2})

WriteResult ({"nRemoved": 3})

Check it out:

> db.t1.find ()

>

There is no data.

This method can delete documents under the collection.

!! How to delete a collection:

> db.t1.drop ()

True

Check it out:

> show tables

System.indexes

There is no T1 document.

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