目录
Working with the native mongodb driver
首先需要下载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是数据库
- 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>
- 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
- }
- ]
- 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
- }
- ]
- db.products.insertOne({
- id:1,
- name:"Pen",
- price:1.20,
- reviews:[
- {
- name:"ann",
- ticket:"anue",
- reviews:"excellet"
- },
- {
- name:"Bob",
- ticket:"anue",
- reviews:"great"
- }
- ]
- })
就是在一个文档中,再添加多一点
使用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
