• Mongodb


    目录

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

    ​编辑

    read

    update

    delete

    Relationships in MongoDB

    Working with the native mongodb driver


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

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

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

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

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

    Set current database,创建一个数据库

    1. 27017> use shopDB
    2. switched to db shopDB
    3. shopDB> show dbs
    4. admin 40.00 KiB
    5. config 108.00 KiB
    6. local 80.00 KiB

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

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

    插入后的效果 

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

    shopDB是数据库

    read

    1. shopDB> db.products.find()
    2. [
    3. {
    4. _id: ObjectId("6386229c3c2493acda0fdebc"),
    5. id: 1,
    6. name: 'Pen',
    7. price: 1.2
    8. },
    9. {
    10. _id: ObjectId("63862a353c2493acda0fdebd"),
    11. id: 2,
    12. name: 'Pencil',
    13. price: 0.8
    14. }
    15. ]

    根据名字查询数据

    1. shopDB> db.products.find({name:"Pencil"})
    2. [
    3. {
    4. _id: ObjectId("63862a353c2493acda0fdebd"),
    5. id: 2,
    6. name: 'Pencil',
    7. price: 0.8
    8. }
    9. ]

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

    1. shopDB> db.products.find({price:{$gt:1}})
    2. [
    3. {
    4. _id: ObjectId("6386229c3c2493acda0fdebc"),
    5. id: 1,
    6. name: 'Pen',
    7. price: 1.2
    8. }
    9. ]

    根据两个query查找 

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

    update

    1. shopDB> db.products.updateOne({price:0.8},{$set:{stock:32}})
    2. {
    3. acknowledged: true,
    4. insertedId: null,
    5. matchedCount: 1,
    6. modifiedCount: 0,
    7. upsertedCount: 0
    8. }
    9. shopDB> db.products.find()
    10. [
    11. {
    12. _id: ObjectId("6386229c3c2493acda0fdebc"),
    13. id: 1,
    14. name: 'Pen',
    15. price: 1.2
    16. },
    17. {
    18. _id: ObjectId("63862a353c2493acda0fdebd"),
    19. id: 2,
    20. name: 'Pencil',
    21. price: 0.8,
    22. stock: 32
    23. }
    24. ]

    delete

    1. shopDB> db.products.deleteOne({stock:32})
    2. { acknowledged: true, deletedCount: 1 }
    3. shopDB> db.products.find()
    4. [
    5. {
    6. _id: ObjectId("6386229c3c2493acda0fdebc"),
    7. id: 1,
    8. name: 'Pen',
    9. price: 1.2,
    10. stock: 12
    11. }
    12. ]

    Relationships in MongoDB

    1. db.products.insertOne({
    2. id:1,
    3. name:"Pen",
    4. price:1.20,
    5. reviews:[
    6. {
    7. name:"ann",
    8. ticket:"anue",
    9. reviews:"excellet"
    10. },
    11. {
    12. name:"Bob",
    13. ticket:"anue",
    14. reviews:"great"
    15. }
    16. ]
    17. })

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

    Working with the native mongodb driver

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

    并且使用npm初始化

    MongoDB and Node.js Tutorial - CRUD Operations | MongoDB

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

    1. const { MongoClient } = require("mongodb");
    2. // Connection URI
    3. const uri =
    4. "mongodb://localhost:27017";
    5. // Create a new MongoClient
    6. const client = new MongoClient(uri);
    7. async function main() {
    8. try {
    9. // Connect the client to the server (optional starting in v4.7)
    10. await client.connect();
    11. await createMultipleListings(client, [
    12. {
    13. name: "Infinite Views",
    14. score: 8,
    15. review: "Great fruit"
    16. },
    17. {
    18. name: "Orange",
    19. score: 6,
    20. review: "kinda sour"
    21. },
    22. {
    23. name: "Banana",
    24. score: 9,
    25. review: "Great stuff"
    26. }
    27. ]);
    28. // Establish and verify connection
    29. await client.db("fruitDB").command({ ping: 1 });
    30. console.log("Connected successfully to server");
    31. } finally {
    32. // Ensures that the client will close when you finish/error
    33. await client.close();
    34. }
    35. }
    36. main().catch(console.error);
    37. //Create Multiple Documents
    38. async function createMultipleListings(client, newListings){
    39. const result = await client.db("fruitDB").collection("fruit").insertMany(newListings);
    40. console.log(`${result.insertedCount} new listing(s) created with the following id(s):`);
    41. console.log(result.insertedIds);
    42. }

    connection+insert数据

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

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

    在使用bash启动node.js

     

     

  • 相关阅读:
    知识点3--CMS项目查看文章详情
    算法|203. 移除链表元素 206.反转链表 707. 设计链表
    git ssh permission denied解决办法
    JavaScript数组常用方法解析和深层次js数组扁平化
    Redis主从复制/读写分离的配置
    Python re.findall()中的正则表达式包含多个括号时的返回值——包含元组的列表
    dangzero环境配置问题
    入职字节外包一个月,我离职了
    折磨人的算法题咋解?阿里内部强推超全Java算法学习指南,已被彻底征服,早知道可以少掉我多少头发了
    MySQL基础篇【命名规范】
  • 原文地址:https://blog.csdn.net/weixin_42173016/article/details/128107129