Home  >  Article  >  System Tutorial  >  Common MongoDB operation commands

Common MongoDB operation commands

WBOY
WBOYforward
2024-01-05 19:56:11523browse
Introduction The natural compatibility of MongoDB and JavaScript makes using MongoDB under Node.js extremely comfortable. We usually use ORM tools like mongoose to operate MongoDB. However, manually viewing the database is still useful in many scenarios, such as debugging relationships between models, clearing user tables, and resetting the database.
This article lists these commonly used MongoDB commands. MongoDB documentation: https://docs.mongodb.com/常用的 MongoDB 操作命令 Database Operation

# View database

show dbs

# Switch database

use mydatabase

# Delete the current database

db.dropDatabase()
Collection operations

# View Collection

show collections

# Delete collection

db.users.drop()
Document operations Insert document
db.users.insert({
    name:'harttle',
    url:'http://harttle.com'
})
Query Document

# Query all

db.users.find()

# Conditional query

db.users.find({
    name:'harttle'
})

# Indented output
db.users.find().pretty()

Update documentation
db.users.update({
    name:'harttle'
}, {
    url:'http://harttle.com'    
})
Delete Document

# Delete all

db.users.remove({})

# Conditional deletion

db.users.remove({
    url:'http://harttle.com'
})

The above is the detailed content of Common MongoDB operation commands. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete