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" } }
>

注意:第二條不會擊中:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章