mongdb-tut

  • every thing in mongo is like in json
  • we get a db as sqldb has as well , and then get a collection which is like a table , then we use method to
  1. create a db, collection, and curd which is so-called sql boy

now let us get to start to learn how to use sql mwthods


  1. before: you need to connect a mongodb or just use mongosh, I prefer using docker
  2. create a DB
    use --auto create
  3. delete a DB, specified it
    db.dropDatabase() --and the current db is deleted
  4. show dbs --as title say

  1. create a collection
    db.createCollection(,[options not a must])
  2. show collections – as title say
  3. insert into a collection --using collection name
    db..insert({“somekey”:“somevalue”,…}) – and this is more like json on method
  4. delete a collection
    db..drop()
  5. insert many json to a collection --using insertMany method
    db..insertMany([{“title”:“for exam”,“array”:[“I”,“dont”,“do”]},{same as before}],{with some features})
  6. check collection --using find()
    db..find()[.pretty()]
  7. update collection – this is more shit than js, lets look a exam
    db.col.update({‘title’:‘MongoDB 教程’},{$set:{‘title’:‘MongoDB’}})
  8. remove collection key-value pairs – check a condition
    db.col.remove({‘title’:‘MongoDB 教程’})
  9. conditional find from collection – just skip it, you may no going to use, but the idea is that by conditions such as and, or

now check more thing about mothods, notice that every thing in method is more like a json

  1. sort a collection by key – 1 is asc, -1 is desc – col is coll-name
    db.col.find({},{“title”:1,_id:0}).sort({“likes”:-1})
  2. create index --check fast
    db.col.createIndex({“title”:1}) – so 1 is key like above, there is with options as well
  3. aggregate(聚合), view it as buildin function
    db.mycol.aggregate([{KaTeX parse error: Expected '}', got 'EOF' at end of input: …roup : {_id : "by_user", num_tutorial : {$sum : 1}}}]) – notice that every col which is called data has an field call _id, you see that $ as well,
    view it as conditional select

some thing need to learn, we mostly is using this instead of curd

  1. copy and paste, a clone of collection --for safety and reusable, once a collection is cracked, use clone data set, this is connected to cluster(集群)
  2. slice is another cluster tech, that when there is huge data, a computer can’t receive, slice to multiple computers
  3. dump(backup) and restore
  4. db monitor, check what has happened to mongodb

now it comes to connect to programming language

  1. mostly use: java and js

你可能感兴趣的:(数据库/Oracle数据库,数据库,mongodb,java)