Why are mongodb fields of different types

  mongodb, question

The question I want to ask is, mongodb is a document-oriented database, and its storage format is bson, which is so similar to json. With such a relaxed use environment, why don’t you step up a bit and have type constraints on each field? Such as the following data structure

    'field_1': 1,
    'field_2': '1',
    'field_3': true

In scripting languages, these three values are equal, but mongodb does not think so if you usestringType to findfield_1The value of, then will never find.

But in a weak language like php, it isI can’t tell the difference between a fool and an idiot.So php programmers who use mongodb often see the following cup scenarios. When you store the primary key, it is generated by a functionintHowever, the GET parameter passed from the front end may be used directly when taking out the record. Unfortunately, it can only be a string type, so you will never find this record.

There are still many things like this. I wonder if mongodb doesn’t restrict the data structure, why not even restrict the data type? Since the structure is not constrained, the program cannot correspond to the type, so it is not good to do such things as forced type conversion. Do you have any opinions?

The retrieval efficiency and the storage method can never be evaded. The three types of storage byte length determine that the query and calculation efficiency cannot be consistent. How to optimize nosql is only at the policy level. After all, the storage layer is more concerned with the performance of the media itself.