一文解决mogodb基础

2 篇文章 0 订阅
1 篇文章 0 订阅

MongoDB适用场景。

  • 文档型数据库
  • 高性能
  • 灵活性
  • 可扩展性
  • 强大的查询语句
  • 高性能

MongoDB中的数据存储类型。

  • 数据库
  • 集合
  • 文档

MongoDB的基础语法

show dbs;// 查看数据库列表
db; // 查看当前数据库
use db; // 切换到db数据库
show collections; // 查看集合
db.users.find(); // 查询users表里面的所有数据
db.dropDatabase(); // 删除当前使用的数据库
db.collection.insert(); // 插入数据
db.collection.drop(); // 删除表

// 插入文档
// 插入单条
db.collection.insertOne();
db.collection.insertOne({name:"xmh",age:19});

// 插入多条
db.collection.insertMany();
db.collection.insertMany([{name:"x",age:20},{name:"h",age:25}]);

// 插入单条或者多条
db.collection.insert();
db.collection.insert({name:"xxx",age:200});
db.collection.insert({name:"xmmm",age:50},{name:"du",age:55});

// 查询文档
db.collection.find();// 查询所有
// 查询参数解释
db.collection.find({},{}) // 第一个参数表示查询条件,第二个参数表示查询的值

// 删除文档
db.collection.deleteMany({}); // 满足条件的全部删除
db.collection.deleteOne({}); // 删除第一个满足条件的

// 查询嵌套文档
// 插入测试数据
db.test.insert({
  "item": "journal",
  "qty": 25,
  "size": {
    "h": 14,
    "w": 21,
    "uom": "cm"
  },
  "status": "A"
});
db.test.insert({
  "item": "notebook",
  "qty": 50,
  "size": {
    "h": 8.5,
    "w": 11,
    "com": "in"
  },
  "status": "A"
})
// 完全匹配 包括“h”,“w”位置,切换位置,查询不到数据。
db.test.find({
  "size": {
    "h": 14,
    "w": 21,
    "com": "cm"
  }
});

// 嵌套字段的具体查询 点出里面的成员
db.test.find({
  "size.com":"in"
});

// 字段包含字母进行查询
db.test.drop(); 
//插入测试数据
db.test.insertMany([
  { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [14, 21] },
  { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [14, 21] },
  { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [14, 21] },
  { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [22.85, 30] },
  { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [10, 15.25] }
]);
// 查询数组里面的内容,有顺序
db.test.find({
   tags:["red","blank"],
});
// 查询数据中是不是包含这个数据 无序 只要包含就行
db.test.find({
   tags:{$all:["red","blank"]}
});
// 查询数据中是不是包含这个数据
db.test.find({
   tags:"red",
});
// 查询dim_cm中的有值大于25的数据
db.test.find({ 
   dim_cm:{$gt:25}
});
// dim_cm中的值大于25,小于50
db.test.find({
   dim_cm:{$elemMatch:{$gt:25,$lt:50}}
});
// 查询dim_cm中的第二个元素的值大于25,
db.test.find("dim_cm.1":{$gt:25}});

// 查询嵌入文档数组
db.test.drop();
// 插入数据
db.test.insertMany([
  {
    item: "journal",
    instock: [{ warehouse: "A", qty: 5 },
    { warehouse: "C", qty: 15 }]
  },
  {
    item: "notebook",
    instock: [{ warehouse: "A", qty: 60 },
    { warehouse: "B", qty: 15 }]
  },
  {
    item: "paper",
    instock: [{ warehouse: "A", qty: 40 },
    { warehouse: "B", qty: 5 }]
  },
  {
    item: "postcard",
    instock: [{ warehouse: "B", qty: 15 },
    { warehouse: "C", qty: 35 }]
  }
]);

// 查询instock中有下面这个元素的文档
db.test.find({
   "instock":{warehouse:"A",qty:5}
});
// 查询包含qty小于20的文档
db.test.find({
   "instock.qty":{$lt:20}
});
// 查询instock中第一项的qty小于20的数据
db.test.find({
   "instock.0.qty":{$lt:20}
});
// 查询多个条件
db.test.find({
  "instock": {
    $elemMatch: {
      qty: 5,
      warehouse: "A"
    }
  }
});
db.test.find({
  "instock": {
    $elemMatch: {
      qty: { $gt: 10, $lte: 20 }
    }
  }
});
// 任意一个
db.test.find({
  "instock.qty": { $gt: 10, $lte: 20 }
});
db.test.find({
  "instock.qty": 5,
  "instock.warehouse": "A"
});

// 查询指定集合里面的字段
// 1:要,0:不要
db.test.find({}, {item:1});// 默认包含_id,item
db.test.find({}, {item:1,_id:0});// 返回的数据只包含item字段
db.test.find({}, {item:0});// 除去item字段的所有字段
db.test.find({},{item:1,status:1,"instock.warehouse":1});
// 禁止嵌入文档中的特定字段
db.test.find({}, {
   "instock.warehouse":0
});
// 数组中的最后一个元素slice
db.test.find({}, {"instock":{$slice:-1}});
// 查询item为null
// 错误示例:
db.test.find({item:null}); // item为null 和item不存在都会展现出来
// 类型检查 10 指的是 null 类型 没有item是查询不到的
db.test.find({ item: { $type: 10 } });
// 存在检查 不存在item时
db.test.find({ item: { $exists: false } });

// updateOne updateMany replaceOne 更新数据
db.test.drop();
db.test.insert({
  "item": "journal",
  "qty": 25,
  "size": {
    "h": 14,
    "w": 21,
    "uom": "cm"
  },
  "status": "A"
});

db.test.insert({
  "item": "notebook",
  "qty": 50,
  "size": {
    "h": 8.5,
    "w": 11,
    "com": "in"
  },
  "status": "A"
});

// 更新字段 更新字段不存在 直接创建
// 把item = notebook的size.com 更新为cm,status更新为P
// lastModified 被创建 
db.test.updateOne({
   { item : "notebook"},
   { 
     $set:{"size.com":"cm",status:"P"},
     $currentDate: {lastModified: true},
   });
// 更新所有
db.test.updateMany({
   { item : "notebook"},
   { 
     $set:{"size.com":"cm",status:"P"},
     $currentDate: {lastModified: true},
   });
// 替换文档 替换掉item=notebook _id不可替换
db.test.replaceOne(
  { item: "notebook" },
  { item: "paper", instock: [{ warehouse: "A", qty: 60 }, { warehouse: "B", qty: 40 }] }
);
// 删除文档
db.collection.deleteMany({}); // 删除多个文档 第一个参数表条件
db.collection.deleteOne({}); // 删除单个文档 第一个参数表条件

egg + mongoose RESTful api设计实例

请看:https://blog.csdn.net/xmh_1997/article/details/123152153
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值