mongo 课程笔记之基本操作

插入

插入一条数据
> db.fruit.insertOne({"name": "Apple"})
{
	"acknowledged" : true,
	"insertedId" : ObjectId("5e9abd1db647be1aca615bb4")
}

acknowledged 表示插入成功,
insertedId 是该插入数据的 _id 字段。

插入多条数据
> db.fruit.insertMany([{name: "apple"},{name: "pear"},{name: "orange"}])
{
	"acknowledged" : true,
	"insertedIds" : [
		ObjectId("5e9abdb1b647be1aca615bb5"),
		ObjectId("5e9abdb1b647be1aca615bb6"),
		ObjectId("5e9abdb1b647be1aca615bb7")
	]
}

返回同理, acknowledged 表示插入成功。
insertedIds 是插入数据的 _id 字段列表。

查询

关于 find
  • find 是 mongo 中查询数据的基本命令,相当于 sql 中的 select。
  • find 返回的是游标。
find 示例

在这里插入图片描述
在这里插入图片描述

> db.fruit.find()
{ "_id" : ObjectId("5e9abd1db647be1aca615bb4"), "name" : "Apple" }
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb5"), "name" : "apple" }
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb6"), "name" : "pear" }
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb7"), "name" : "orange" }


> // 单条件查询
> db.fruit.find({"name": "apple"})
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb5"), "name" : "apple" }

> // 多条件 and 进行查询
> db.fruit.find({"name": "apple", "_id": ObjectId("5e9abdb1b647be1aca615bb5")})
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb5"), "name" : "apple" }

> // 另外一种 and 逻辑查询
> db.fruit.find({$and:[ {"name":"apple"}, {"_id": ObjectId("5e9abdb1b647be1aca615bb5")} ]})
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb5"), "name" : "apple" }

> // or 查询
> db.fruit.find({"$or":[{"name": "apple"}, {"name": "Apple"} ] } )
{ "_id" : ObjectId("5e9abd1db647be1aca615bb4"), "name" : "Apple" }
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb5"), "name" : "apple" }

> // 按照正则表达式进行查找
> db.fruit.find({"name": /^a/})
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb5"), "name" : "apple" }

> db.fruit.find({"name": /^a|A/})
{ "_id" : ObjectId("5e9abd1db647be1aca615bb4"), "name" : "Apple" }
{ "_id" : ObjectId("5e9abdb1b647be1aca615bb5"), "name" : "apple" }

查询中的比较:

> // 大于
> db.orders.find({"userId":{"$gt": 3300}}, {"userId": 1}).next()
{ "_id" : ObjectId("5dbe7a545368f69de2b4d36e"), "userId" : 3573 }
> // 大于等于
> db.orders.find({"userId":{"$gte": 3300}}, {"userId": 1}).next()
{ "_id" : ObjectId("5dbe7a545368f69de2b4d36e"), "userId" : 3573 }


> // 小于
> db.orders.find({"userId":{"$lt": 3300}}, {"userId": 1}).next()
{ "_id" : ObjectId("5dbe7a545368f69de2b4d36f"), "userId" : 2500 }
> // 小于等于
> db.orders.find({"userId":{"$lte": 3300}}, {"userId": 1}).next()
{ "_id" : ObjectId("5dbe7a545368f69de2b4d36f"), "userId" : 2500 }


> // 等于
> db.orders.find({"userId":{"$eq": 3300}}, {"userId": 1}).next()
{ "_id" : ObjectId("5dbe7a5850fc769de3e19f33"), "userId" : 3300 }
> db.orders.find({"userId":3300}, {"userId": 1}).next()
{ "_id" : ObjectId("5dbe7a5850fc769de3e19f33"), "userId" : 3300 }
> // 不等于
> db.orders.find({"userId":{"$ne": 3300}}, {"userId": 1}).next()
{ "_id" : ObjectId("5dbe7a545368f69de2b4d36e"), "userId" : 3573 }

在这里插入图片描述

在这里插入图片描述

使用 find 搜索子文档
> db.fruit.insertOne({"name": "apple", "from": {"country": "China", "province": "Henan"}} )
{
	"acknowledged" : true,
	"insertedId" : ObjectId("5e9ac726b647be1aca615bb9")
}

> db.fruit.find({"name": {"$in": ["apple", "Apple"]}, "from.country": "China"})
{ "_id" : ObjectId("5e9ac726b647be1aca615bb9"), "name" : "apple", "from" : { "country" : "China", "province" : "Henan" } }
>

注意:第二条不会击中:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值