MongoDB syntax

MongoDB is a document-oriented NoSQL database, and its syntax is different from that of relational databases such as MySQL or PostgreSQL. Here are some examples of MongoDB syntax:

1、Creating a collection:

To create a collection in MongoDB, use the db.createCollection() method:

db.createCollection("myCollection")

2、Inserting documents:

To insert documents into a collection, use the insertOne() or insertMany() method:

db.myCollection.insertOne({name: "John", age: 30})
db.myCollection.insertMany([{name: "Jane", age: 25}, {name: "Bob", age: 35}])

3、Querying documents:

To query documents from a collection, use the find() method:

db.myCollection.find({name: "John"})

This will return all documents in the myCollection collection where the name field is “John”.

4、Updating documents:

To update a document in a collection, use the updateOne() or updateMany() method:

db.myCollection.updateOne({name: "John"}, {$set: {age: 40}})

This will update the first document in the myCollection collection where the name field is “John”, setting the age field to 40.

5、Deleting documents:

To delete documents from a collection, use the deleteOne() or deleteMany() method:

db.myCollection.deleteOne({name: "John"})

This will delete the first document in the myCollection collection where the name field is “John”.

你可能感兴趣的:(MongoDB,mongodb,数据库)