1. Detailed explanation of Insert operation
db.collection.insertOne()
inserts multiple documents, db.collection.insertMany()
db.inventory.insertMany([
{
item: "journal", qty: 25, size: {
h: 14, w: 21, uom: "cm" }, status: "A" },
{
item: "notebook", qty: 50, size: {
h: 8.5, w: 11, uom: "in" }, status: "A" },
{
item: "paper", qty: 100, size: {
h: 8.5, w: 11, uom: "in" }, status: "D" },
{
item: "planner", qty: 75, size: {
h: 22.85, w: 30, uom: "cm" }, status: "D" },
{
item: "postcard", qty: 45, size: {
h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
2. Detailed Query operation
Query all, db.collection.find(), equivalent to: SELECT * FROM table_name
query db.collection.find({key:value}) according to conditions, equivalent to SELECT * FROM table_name WHERE name =?
3. Update a single document db .collection.updateOne()
Update multiple documents db.collection.updateMany()
4. Replace the document db.collection.replaceOne().
5. Delete all documents db.collection.deleteMany()
比较操作符"$gt" 、"$gte"、 "$lt"、 "$lte"(分别对应">"、 ">=" 、"<" 、"<=")
$ne 对应 不等于
如:某集合B集合中文档有属性x值为整数,需查找10<x<=30的文档,写法如下:
db.B.find({
"x":{
"$gt":10,"$lte":30}})
$exists 判断文档属性是否存在
db.B.find({
"name":{
"$exists":true}}) --查找属性name存在的文档
db.B.find({
"name":{
"$exists":false}}) --查找属性name不存在的文档