本文主要是介绍CP2 Importing ,Exporting,and Querying Data,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
How Does MongoDB Store Data?
What is JSON?
JavaScript Object Notation, more commonly known as JSON,was defined as part of the JavaScript language in the early 2000s.
JavaScript objects are simple associative containers, wherein a string key is mapped to a value (which can be a number, string, function, or even another object).
JSON shows up in many different cases:
- APIs
- Configuration files
- Log messages
- Database storage
JSON quickly overtook XML, is more difficult for a human to read, significantly more verbose, and less ideally suited to representing object structures used in modern programming languages.
there are several issues that make JSON less than ideal for usage inside of a database.
-
JSON is a text-based format, and text parsing is very slow
-
JSON’s readable format is far from space-efficient, another database concern
-
JSON only supports a limited number of basic data types
In order to make MongoDB JSON-first, but still high-performance and general-purpose, BSON was invented to bridge the gap: a binary representation to store data in JSON format, optimized for speed, space, and flexibility. It’s not dissimilar from other interchange formats like protocol buffers, or thrift, in terms of approach.
What is BSON?
BSON simply stands for “Binary JSON,” and that’s exactly what it was invented to be. BSON’s binary structure encodes type and length information, which allows it to be parsed much more quickly.
Since its initial formulation, BSON has been extended to add some optional non-JSON-native data types, like dates and binary data, without which MongoDB would have been missing some valuable support.
Languages that support any kind of complex mathematics typically have different sized integers (ints vs longs) or various levels of decimal precision (float, double, decimal128, etc.).
Not only is it helpful to be able to represent those distinctions in data stored in MongoDB, it also allows for comparisons and calculations to happen directly on data in ways that simplify consuming application code.
Does MongoDB use BSON, or JSON?
MongoDB stores data in BSON format both internally, and over the network, but that doesn’t mean you can’t think of MongoDB as a JSON database. Anything you can represent in JSON can be natively stored in MongoDB, and retrieved just as easily in JSON.
JSON vs BSON
Schema Flexibility and Data Governance
One of the big attractions for developers using databases with JSON and BSON data models is the dynamic and flexible schema they provide when compared to the rigid, tabular data models used by relational databases.
Firstly, JSON documents are polymorphic – fields can vary from document to document within a single collection
Secondly, there is no need to declare the structure of documents to the database – documents are self-describing. Developers can start writing code and persist objects as they are created.
Thirdly, if a new field needs to be added to a document, it can be created without affecting all other documents in the collection, without updating a central system catalog and without taking the database offline.
Binary JSON, is a binary-encoded serialization of JSON-like documents. Like JSON, BSON supports the embedding of documents and arrays within other documents and arrays. BSON also contains extensions that allow representation of data types that are not part of the JSON spec.
BSON was designed to have the following three characteristics :
1.Lightweight
2.Traversable
3.Efficient
Importing and Export Data
mongodump --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies"
mongoexport --uri="mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies" --collection=sales --out=sales.json
mongorestore --uri "mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies" --drop dump
mongoimport --uri="mongodb+srv://<your username>:<your password>@<your cluster>.mongodb.net/sample_supplies" --drop sales.json
Which of the following commands will add a collection that is stored in animals.json to an Atlas cluster?
- Mongoimport
Data Explorer
Namespace - The concatenation of the database name and collection name is called a namespace.
We looked at the sample_training.zips collection and issued the following queries:
- {"state": "NY"}
- {"state": "NY", "city": "ALBANY"}
In this lesson we used the following commands:
Connect to the Atlas cluster:
mongo "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/admin"
show dbs
use sample_training
show collections
db.zips.find({"state": "NY"})
db.zips.find({"state": "NY"}).count()
db.zips.find({"state": "NY", "city": "ALBANY"})
db.zips.find({"state": "NY", "city": "ALBANY"}).pretty()
Which of the following statements are true about the mongo shell?
It is a fully functioning JavaScript interpreter
It allows you to interact with your MongoDB instance without using a Graphical Interface.
{"start station name" : "Howard St & Centre St", "birth year" : 1961}
这篇关于CP2 Importing ,Exporting,and Querying Data的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!