Mongodb

本文档详细介绍了如何使用shell连接MongoDB数据库,包括显示数据库、切换数据库、创建和插入文档、查询、更新及删除数据的操作。此外,还展示了在MongoDB中处理文档内关系以及使用MongoDB原生驱动进行数据操作的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

如何使用shell连接上mongodb数据库?

​编辑

read

update

delete

Relationships in MongoDB

Working with the native mongodb driver


如何使用shell连接上mongodb数据库?

首先需要下载command line,mongodb配套的mongodb sh。

直接输入一个string(27017),所以直接就连上这个数据 

呈现数据库的名字和存储空间

27017> show dbs
admin   40.00 KiButError: [COMMON-10001] 'undefined' is not a valid argument for "show".
config  60.00 KiB
local   80.00 KiB

Set current database,创建一个数据库

27017> use shopDB
switched to db shopDB
shopDB> show dbs
admin    40.00 KiB
config  108.00 KiB
local    80.00 KiB

创建一个文档,并插入数据

shopDB> db.products.insertOne({
... id:1,name:"Pen",price:1.20})
{
  acknowledged: true,
  insertedId: ObjectId("6386229c3c2493acda0fdebc")
}

插入后的效果 

collections是集合(products),有点像数据表

shopDB是数据库

read

shopDB> db.products.find()
[
  {
    _id: ObjectId("6386229c3c2493acda0fdebc"),
    id: 1,
    name: 'Pen',
    price: 1.2
  },
  {
    _id: ObjectId("63862a353c2493acda0fdebd"),
    id: 2,
    name: 'Pencil',
    price: 0.8
  }
]

根据名字查询数据

shopDB> db.products.find({name:"Pencil"})
[
  {
    _id: ObjectId("63862a353c2493acda0fdebd"),
    id: 2,
    name: 'Pencil',
    price: 0.8
  }
]

使用指令找price:{$gt:1} 

shopDB> db.products.find({price:{$gt:1}})
[
  {
    _id: ObjectId("6386229c3c2493acda0fdebc"),
    id: 1,
    name: 'Pen',
    price: 1.2
  }
]

根据两个query查找 

shopDB> db.products.find({id:1},{name:1})
[ { _id: ObjectId("6386229c3c2493acda0fdebc"), name: 'Pen' } ]
shopDB>

update

shopDB> db.products.updateOne({price:0.8},{$set:{stock:32}})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 0,
  upsertedCount: 0
}
shopDB> db.products.find()
[
  {
    _id: ObjectId("6386229c3c2493acda0fdebc"),
    id: 1,
    name: 'Pen',
    price: 1.2
  },
  {
    _id: ObjectId("63862a353c2493acda0fdebd"),
    id: 2,
    name: 'Pencil',
    price: 0.8,
    stock: 32
  }
]

delete

shopDB> db.products.deleteOne({stock:32})
{ acknowledged: true, deletedCount: 1 }
shopDB> db.products.find()
[
  {
    _id: ObjectId("6386229c3c2493acda0fdebc"),
    id: 1,
    name: 'Pen',
    price: 1.2,
    stock: 12
  }
]

Relationships in MongoDB

db.products.insertOne({
id:1,
name:"Pen",
price:1.20,
reviews:[
    {
        name:"ann",
        ticket:"anue",
        reviews:"excellet"
    },
       {
        name:"Bob",
        ticket:"anue",
        reviews:"great"
    }
]
})

就是在一个文档中,再添加多一点

Working with the native mongodb driver

使用bash 语句在command line 建立文件夹,并且创建app.js

并且使用npm初始化

MongoDB and Node.js Tutorial - CRUD Operations | MongoDB

随后导入以下代码到app.js中

const { MongoClient } = require("mongodb");

// Connection URI
const uri =
  "mongodb://localhost:27017";

// Create a new MongoClient
const client = new MongoClient(uri);

async function main() {
  try {
    // Connect the client to the server (optional starting in v4.7)
    await client.connect();

    await createMultipleListings(client, [
    {
        name: "Infinite Views",
        score: 8,
        review: "Great fruit"
    },
    {
        name: "Orange",
        score: 6,
        review: "kinda sour"
    },
    {
        name: "Banana",
        score: 9,
        review: "Great stuff"
    }
]);
    // Establish and verify connection
    await client.db("fruitDB").command({ ping: 1 });
    console.log("Connected successfully to server");
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
main().catch(console.error);


//Create Multiple Documents
async function createMultipleListings(client, newListings){
    const result = await client.db("fruitDB").collection("fruit").insertMany(newListings);

    console.log(`${result.insertedCount} new listing(s) created with the following id(s):`);
    console.log(result.insertedIds);       
}

connection+insert数据

源代码:nodejs-quickstart/create.js at master · mongodb-developer/nodejs-quickstart · GitHub

随后打开mongo自带的sh,并且连接上数据库

在使用bash启动node.js

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值