首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

MongoDB装配及增删改查操作

2013-10-22 
MongoDB安装及增删改查操作Mon Dec 24 15:17:03 [initandlisten] waiting for connections on port 27017M

MongoDB安装及增删改查操作

  • Mon Dec 24 15:17:03 [initandlisten] waiting for connections on port 27017
  • Mon Dec 24 15:17:03 [websvr] admin web console waiting for connections on port 2
  • 8017

    ?

    2. 连接MongoDB

    再打开一个命令行,执行以下mongo命令:

    ?

  • : "123456" }
  • >

    ?

    ?

    以下是三个常用的命令

    ?

  • { "_id" : ObjectId("50d8046a3cc996b4e8ce22df"), "username" : "google", "password" : "google123" }


    5. 更新记录

    更新一条记录语法:

    db.tablename.update({criteria},{$set: {new value}}).

    下面的例子中更新用户henry的密码:

    ?

  • { "_id" : ObjectId("50d803393cc996b4e8ce22de"), "password" : "henry123456", "username" : "henry" }
  • >

    ?

    ?

    6. 查询记录

    查找记录的语法:

    db.tablename.find({criteria}).

    6.1 列出users表中的所有记录

    ?

  • { "_id" : ObjectId("50d803393cc996b4e8ce22de"), "password" : "henry123456", "username" : "henry" }
  • >

    ?

    ?

    6.2 查询username 为henry的记录

    ?

    ?

    ?

    7. 删除记录

    删除记录的语法:

    db.tablename.remove({criteria}).

    下面的例子

    ?

  • >

    ?

    ?

    Note

    To delete all records from a table, uses db.tablename.remove().
    To drop the table, uses db.tablename.drop().

    8. 索引

    Index(索引)可以提高查询的效率

    8.1 List all indexes of table “users”, by default the column “_id” is always the primary key and created automatically.

    ?

  • "key" : {
  • "_id" : 1
  • },
  • "ns" : "mydb.users",
  • "name" : "_id_"
  • }
  • ]
  • >

    ?

    ?

    8.2 To create an index, uses db.tablename.ensureIndex(column). In below example, an index is created on column “username”.

    ?

  • "key" : {
  • "_id" : 1
  • },
  • "ns" : "mydb.users",
  • "name" : "_id_"
  • },
  • {
  • "v" : 1,
  • "key" : {
  • "username" : 1
  • },
  • "ns" : "mydb.users",
  • "name" : "username_1"
  • }
  • ]
  • >

    ?

    ?

    8.3 To drop an index, uses db.tablename.dropIndex(column). In below example, the index on column “username” is deleted or dropped.

    ?

  • > db.users.getIndexes()
  • [
  • {
  • "v" : 1,
  • "key" : {
  • "_id" : 1
  • },
  • "ns" : "mydb.users",
  • "name" : "_id_"
  • }
  • ]
  • >

    ?

    ?

    8.4 To create an unique index, uses db.tablename.ensureIndex({column},{unique:true}). In below example, an unique index is created on column “username”.

    ?

  • "key" : {
  • "_id" : 1
  • },
  • "ns" : "mydb.users",
  • "name" : "_id_"
  • },
  • {
  • "v" : 1,
  • "key" : {
  • "username" : 1
  • },
  • "unique" : true,
  • "ns" : "mydb.users",
  • "name" : "username_1"
  • }
  • ]
  • >

    ?

    ?

    10. 帮助

    At last, uses help() to guide you how to do things in MongoDB.

    10.1 help – All available commands.

    ?

  • > db.users.find().help()
  • find() modifiers
  • .sort( {...} )
  • .limit( n )
  • .skip( n )
  • .count() - total # of objects matching query, ignores skip,limit
  • .size() - total # of objects cursor would return, honors skip,limit
  • .explain([verbose])
  • //...

    ?

    Done. Hope this summary of MongoDB commands could help others.

    References

    Official MongoDB tutorials
    MongoDB Indexes

    http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/

  • 热点排行