0
点赞
收藏
分享

微信扫一扫

Insert Documents

小龟老师 2022-02-18 阅读 46



​_id​​ Field

In MongoDB, each document stored in a collection requires a unique ​​_id​​​ field that acts as a ​​primary key​​​. If an inserted document omits the ​​_id​​​ field, the MongoDB driver automatically generates an ​​ObjectId​​​ for the ​​_id​​ field

插入数据

db.inventory.insertOne(
{ item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }
)

查询

db.inventory.find( {} )


# 根据item查询
db.inventory.find( { item: "canvas" } )

插入多条数据

db.inventory.insertMany([
{ item: "journal", qty: 25, tags: ["blank", "red"], size: { h: 14, w: 21, uom: "cm" } },
{ item: "mat", qty: 85, tags: ["gray"], size: { h: 27.9, w: 35.5, uom: "cm" } },
{ item: "mousepad", qty: 25, tags: ["gel", "blue"], size: { h: 19, w: 22.85, uom: "cm" } }
])


# 总条数
db.inventory.find({}).size()

# limit
db.inventory.find({}).limit(2)

 

举报

相关推荐

0 条评论