当前位置:Gxlcms > 数据库问题 > MongoDB入门

MongoDB入门

时间:2021-07-01 10:21:17 帮助过:1人阅读

 

启动服务    mongod --dbpath C:\data\db 

启动客户端(shell)   mongo 


 

基本概念

SQL术语/概念MongoDB术语/概念解释/说明
database database 数据库
table collection 数据库表/集合
row document 数据记录行/文档           {key: value}
column field 数据字段/域
index index 索引
table joins   表连接,MongoDB不支持
primary key primary key 主键,MongoDB自动将_id字段设置为主键

  


 

数据库操作( db.help() )

  show dbs  显示所有数据库

  db  显示当前所在数据库

  use testdb  切换到tesdbt数据库,没有则新建testdb数据库

  db.dropDatabase() 删除当前所在数据库

 

 


 

 集合操作( db.mycoll.help() )

  show collections 显示所有集合

  db.createCollection(name, { size : ..., capped : ..., max : ... } )  创建集合

     db.coll.insert({‘xx‘:‘xx‘,...})  向coll集合中插入文档,coll不存在则新建

     db.coll.find()  查看coll集合中的所有文档,find(m,n)可以指定查询个数,find().sort() 可以连接使用,还有其他的.size(), .count()...

  db.coll.drop()  删除coll集合

 


 

用Python操作MongoDB

pip install pymongo

import pymongo


client = pymongo.MongoClient()   可以传递参数 (‘localhost‘, 27017)  or  (‘mongod://localhost:21017/‘)

db = client[testdb]     or  client.testdb

posts = db[posts]       or db.posts

posts.insert({xx:xx, ...})   

posts.find()     

 


 

MongoDB入门

标签:

人气教程排行