1、Insert操作详解
db.collection.insertOne()
插入多个文档,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、Query操作详解
查询所有, db.collection.find(),相当于:SELECT * FROM table_name
按条件查询db.collection.find({key:value}),相当于SELECT * FROM table_name WHERE name = ?
3、更新单个文档db.collection.updateOne()
更新多个文档db.collection.updateMany()
4、替换文档db.collection.replaceOne()。
5、删除所有文档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不存在的文档