[MongoDB]--MongoDB basic notes

1. Introduction to MongoDB

Redis
If you have extremely high requirements for data reading and writing, and your data is not large in scale and does not require long-term storage, choose redis;

MongoDB
is suitable for those tables whose structure changes frequently and field types can be modified at any time. The logical structure of the data is not that complicated, multi-table query operations are not required, and the amount of data is relatively large.
If your data is large in scale and has high requirements on data reading performance, the structure of the data table needs to change frequently, and sometimes you need to do some aggregation queries;
reading efficiency in mongodb is higher than writing;
real-time insertion, updating, and querying are suitable for storage Data in documented format;

ElasticSearch
If you need to construct a search engine or you want to build a high-end data visualization platform, and your data has certain analytical value;

HBase
If you need to store massive data, and the subsequent scale will increase greatly;
hbase is suitable for writing more and reading less by default;
it has larger storage capacity and is suitable for distributed massive data storage;

1. Introduction to MongoDB

Document storage is generally stored in a format similar to json, and the stored content is document type. This also gives you the opportunity to index certain fields and implement certain functions of the relational database.
It is an open source database system based on distributed file storage.
MongoDB stores data as a document, and the data structure consists of key=>value pairs. MongoDB documents are similar to JSON objects. Field values ​​can contain other documents, arrays, and document arrays.
NoSQLBooster for MongoDB data management tool to connect to MongoDB.
The following example mainly builds a stand-alone MongoDB service. The version is version 6.0.6 (mongodb-windows-x86_64-6.0.6-signed.msi). First, we briefly introduce how to use the stand-alone service!

2. MongoDB tool operation

Use tools to connect to the local mongoDB, and then create a user
db.createUser({ user: "root", pwd: "root", roles: [{role: "dbOwner", db: "wwy_test"}] } )



Create the wwwy_test library and then create the w_test1 table
Insert image description here

db.w_test1.insert({
   
    
    "name":"wwy1","age":18});
db.w_test1.insert({
   
    
    "name":"wwy1","age":18,"city":"南京"});
db.getCollection("w_test1").insertOne({
   
    
    _id: "0001",name:"wwy1","age":18,"city":"南京"});
db.getCollection("w_test1").insert([{
   
    
    _id: "0005",name:"wwy1","age":18,"city":"南京"},{
   
    
    _id: "0002",name:"lisi1","age":18,"city":"北京"}]); //id重复会报错
db.getCollection("w_test1").insertMany([{
   
    
    _id: "0003",name:"zhaoliu1","age":12,"city":"南京"},{
   
    
    name:"lisi2","age":18,city:"上海"}]);
db.getCollection("w_test1").save({
   
    
    _id: "0001",name:"wwy111","age":20,"city":"东京"}); //id重复就覆盖

db.w_test1.update({
   
    
    "age":17},{
   
    
    $set:{
   
    
    "age": 15}});  //更新一条
db.w_test1.updateOne({
   
    
    "name":"zhangsan"},{
   
    
    $set:{
   
    
    "name": "wangsi"}}); //更新一条
db.w_test1.updateMany({
   
    
    "name":"wwy1"},{
   
    
    $set:{
   
    
    "name": "zhangsan"}}); //更新所有

db.w_test1.deleteOne({
   
    
    name:"lisi2"});
db.w_test1.deleteMany({
   
    
    name:"lisi2"});

db.w_test1.find();
db.w_test1.find().pretty();
db.w_test1.findOne();
db.w_test1.find({
   
    
    name:"wwy1",age:18});  //类似sql: select * from w_test1 where name='wwy1' and age =18;

db.w_test1.find({
   
    
    '$or':[{
   
    
    age:18},{
   
    
    name:"wwy1"}]}); //或者
db.w_test1.find({
   
    
    "age" : {
   
    
    $gt : 16}});  //大于  类似sql: select * from w_test1 where age >16;
db.w_test1.find({
   
    
    "age" : {
   
    
    $gt : 16,$lt:19}},{
   
    
    name:1,age:1});  //类似sql: select name,age from w_test1 where age >16 and age<19;

db.w_test1.find(<

Guess you like

Origin blog.csdn.net/xunmengyou1990/article/details/130745712