当前位置:网站首页>Use of mongodb

Use of mongodb

2022-06-13 05:15:00 strive_ one

Get the database currently in use :

db.getName()

Check the existing database ( Show all databases ):

show databases
#  perhaps 
show dbs

Select database

use  Database name 

Delete database

#  Select the database first 
use  Database name 
#  Then delete the database 
db.dropDatabase()

Check the database status

db.stats()

View database related help information

db.help()

View the existing collections in the database

#  You should enter the database before viewing the collection : use  database 
show tables
#  perhaps 
show collections

Set creation

  • Be careful : If use A nonexistent database , You can still create collections in it
use  database 
db.createCollection(" Collection name ")
#  View set 
show collections

Delete of collection

db. Collection name .drop()
  • Be careful : If you delete a collection that does not exist , Then return to false

Write data

The name of the collection :

  • It cannot be an empty string (" ")
  • Can not contain \0 character ( Null character )
  • Out of commission system. The prefix of ( System reservation )
  • It is not recommended to include reserved words ”$”
  • use . Split subsets of different namespaces ( Such as :blog.users, blog.posts)

Normal add data

use  database 
db.createCollection(" Collection name ")
#  Insert a piece of data 
db. Collection name .insert({
    key1:value1, key2:value2})
#  Insert multiple data 
db. Collection name .insert([{
    key1:value1, key2:value2},{
    key1:value1, key2:value2}])
db. Collection name .find()
  • explain : After the record is successfully inserted ,monogdb One will be added for each document by default _id Field , And in the same set _id Is the only one.
  • The _id The content value of the field yes mongodb It's the result of its own algorithm , The _id The corresponding value information is in ” The only global ”
    It's equivalent to us mysql The primary key in the table id, Is the only one. .
  • advantage :mongo We need to upgrade our data 、 It's easier to migrate

Add more for data objects

db. Collection name .insert({
    key1:value1, key2:[value1,value2]})
db. Collection name .insert({
    key1:value1, key2:{
    value1,value2}})

Save the data

db. Collection name .save({
    key1:value1, key2:value2})
  • characteristic :_id Add if data does not exist , Modify if data exists .

Data query

db. Data sheet .find()
原网站

版权声明
本文为[strive_ one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280514548470.html