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

How to fragment a collection in mongodb

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to slice a collection in mongodb". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In this example, test1 is the database and test1 is the collection name.

1. Enable sharding first, and enable the sharding function of the database

Mongos > use admin

Mongos > db.runCommand ({"enablesharding": "test1"})

2. To slice a specific collection, select the slice key as "_ id"

Mongos > db.runCommand ({"shardcollection": "test1.test1", "key": {"_ id": 1}})

{"collectionsharded": "test1.test1", "ok": 1}

3. Observe the number of shard_01 shard_02 tunks in 2 fragments

= MongoDB= replication set read and write settings =

Write concern

By default, Primary returns as soon as it completes the write operation, and Driver can set the rule for success by setting [Write Concern].

The following write concern rule setting must be successful on most nodes with a timeout of 5s.

Cfg = rs.conf ()

Cfg.settings = {}

Cfg.settings.getLastErrorDefaults = {w: "majority", wtimeout: 5000}

Rs.reconfig (cfg)

Data synchronization

Data is synchronized between Primary and Secondary through oplog. After the write operation on Primary is completed, an oplog,Secondary is continuously fetched new oplog from Primary and applied to a special set of local.oplog.rs.

Because the data of oplog will continue to increase, local.oplog.rs is set as a capped collection, and when the capacity reaches the upper limit of the configuration, the oldest data will be deleted. In addition, considering that oplog may be repeatedly applied on Secondary, oplog must be idempotent, that is, repeated applications will get the same results.

The following oplog format, including ts, h, op, ns, o, and other fields

{

Ts: Timestamp (1446011584, 2)

"h": NumberLong ("1687359108795812092")

"v": 2

"op": "I"

"ns": "test.nosql"

"o": {"_ id": ObjectId ("563062c0b085733f34ab4129"), "name": "mongodb", "score": "100"}

}

}

Ts: operation time, current timestamp + counter, counter is reset every second

H: globally unique identification of the operation

V:oplog version Information

Op: type of operation

I: insert operation

U: update operation

D: delete operation

C: execute commands (such as createDatabase,dropDatabase)

N: null operation, special purpose

Ns: the collection for which the operation is directed

O: operation content, if it is an update operation

O2: the operation queries the where condition. Only the update operation contains this field.

= oplog and journal log problems = Zhang Youdong blog =

When writing a document in a MongoDB replication set, you need to modify the following data

Write document data to the corresponding collection

Update all index information for the collection

Write an oplog for synchronization

The above three modification operations need to ensure that either they are successful or all fail, and that there can be no partial success.

When writing data, MongoDB will put the above three operations into a wiredtiger transaction to ensure "atomicity"

When wiredtiger commits a transaction, all modification operations will be applied and the above three operations will be written to a journal operation log; the backend will periodically checkpoint to persist changes and remove useless journal.

Who writes first?

Oplog and journal are different levels of concepts in MongoDB, and it is unreasonable to compare them together.

Oplog is an ordinary collection in MongoDB, so writing to oplog is no different from writing to a normal collection.

One write will correspond to data, index, and oplog modifications, and these three modifications will correspond to a journal operation log.

This is the end of the content of "how to fragment a collection in mongodb". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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