This is the 24th day of my participation in the August Text Challenge.More articles in August
preface
MySQL, which we often use, is the most popular relational database management system. With the progress of The Times and the development of the Internet, relational database has not met the needs of the Internet, so non-relational database appears. This paper will introduce the MongoDB common query statements.
The initial mongo
MongoDB is a database based on distributed file storage. Written in C++ language. Designed to provide scalable high-performance data storage solutions for WEB applications.
MongoDB is a product between relational database and non-relational database. Among non-relational databases, it has the most rich functions and is the most like relational database.
- Database: indicates the database in SQL.
- Collection: indicates a database table/collection.
- Document: Data record row/document, SQL meaning row.
- Field: data field/field, column in SQL.
- Index: indicates the index in SQL.
- Primary key: indicates the primary key. MongoDB automatically sets the _ID field as the primary key.
Mongo characteristics
- MongoDB is a document storage oriented database, operation is relatively simple and easy.
- You can index any attribute in the MongoDB record (e.g. FirstName=”Sameer”,Address=”8 Gandhi Road”) for faster sorting.
- You can create data images locally or over the network, which makes MongoDB more scalable.
- If the load increases (requiring more storage space and more processing power), it can be distributed to other nodes in the computer network this is called sharding.
- Mongo supports rich query expressions. Query instructions use JSON-style tags to easily query objects and arrays embedded in documents.
- MongoDb uses the update() command to replace completed documents (data) or some specified data fields.
- Map/ Reduce in Mongodb is used to process and aggregate data in batches.
- Map and Reduce. The Map function invokes emit(key,value) to traverse all records in the set and send the key and value to the Reduce function for processing.
- The Map and Reduce functions are written in Javascript, and mapReduce operations can be performed using db.runCommand or mapReduce command.
- GridFS is a built-in feature in MongoDB that can be used to store a large number of small files.
- MongoDB allows script execution on the server side. You can write a function in Javascript and execute it directly on the server side. You can also store the function definition on the server side and call it directly next time.
- MongoDB supports a variety of programming languages :RUBY, PYTHON, JAVA, C++, PHP, C# and more.
- MongoDB is easy to install.
Quick start
Query all
Query all data of a database table/collection
db.getCollection("test").find();
Copy the code
Query the numeric type of a specified field
Query data numeric type based on the specified field userId
db.getCollection("test").find({"userId":632});
Copy the code
Query the string type of a specified field
Query the data string type based on the goodsNo specified field
db.getCollection("test").find({"goodsNo":"789789789789"});
Copy the code
Multi-condition query
Query information about a specified field using multiple conditions
db.getCollection("test").find({"userId":632."supplyGoodsNo":"870000065481"});
Copy the code
Query information about a specified field in a full table
To query the specified field in a full table, return the primary key _ID
db.getCollection("test").find({},{"userId":1."supplyGoodsNo":1."url":1});
Copy the code
Query information about a specified field in a full table without returning primary key _ID
db.getCollection("test").find({},{"userId":1."supplyGoodsNo":1."url":1."_id":0});
Copy the code
Query the specified range of data in a dataset
Query data of a specified range in the data set. The value is greater than or equal to < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < gTE > <= < < gTE <= <= < gTE <= <= < gTE <= <= < gTE <= <= < lte > no! = “$ne”
db.getCollection("test").find({"userId": {"$gte":500."$lte":800}});
Copy the code
Query unequal data information
Query unequal data information
db.getCollection("test").find({"userId": {"$ne":500}});
Copy the code
In contains
In contains some data
db.getCollection("test").find({"userId": {"$in": [500.600.632]}});
Copy the code
Not in Not included
Not in does not contain some data
db.getCollection("test").find({"userId": {"$nin": [123.500.4000]}});
Copy the code
The or or
Select * form test WHERE userId = 632 or supplyGoodsNo = “870000065481
db.getCollection("test").find({"$or": [{"userId":632}, {"supplyGoodsNo":"870000065481"}]});
Copy the code
The mod modulus
Select * from test WHERE (userId mod 5) = 1
db.getCollection("test").find({"userId": {"$mod": [5.1]}});
Copy the code
not
The not statement query is equivalent to the SQL select * from test WHERE NOT (userId = 600) statement.
db.getCollection("test").find({"$not": {"userId":600}});
Copy the code
Empty query
An empty query is equivalent to the SQL select * from test where userId is NULL statement.
db.getCollection("test").find({"userId": {"$in": [null]."$exists":true}});
Copy the code
Regular query
Regular query
db.getCollection("test").find({"userId" : / 63? /i});
Copy the code
An array of query
Query an array containing both “A” and “b” records in the field URL
db.getCollection("test").find({url: {$all: ["a"."e"]}});
Copy the code
Query array, the fourth element (starting from 0) in the field URL is the record of a
db.getCollection("test").find({url3.."a"});
Copy the code
$size = $size; $size = $size; $size = $size
db.getCollection("test").find({"url" : {"$size" : 3}});
Copy the code
A little time
Compare the time size, data after a certain time period (Method 1)
db.getCollection("test").find({"createTime" : {"$gte" : ISODate("The 2021-08-12 16:03:06. 815")}});
Copy the code
Compare the time size, data after a certain time period (Method 2)
db.getCollection("test").find({"createTime": {$gte:new Date(2021.7.12)}});
Copy the code
conclusion
About the author: [Little Ajie] a love tinkering with the program ape, JAVA developers and enthusiasts. Public number [Java full stack architect] maintainer, welcome to pay attention to reading communication.
Well, thank you for reading, I hope you like it, if it is helpful to you, welcome to like collection. If there are shortcomings, welcome comments and corrections. See you next time.