• DataBase -> Collection -> Document -> Field,其中 Field 中又可以保存其他 Document

  • 默认安装好后可以直接登录,无需账号密码,用户是应用在特定的数据库上
use demo_database;
db.createUser(
  {
    user: "demoUser",
    pwd: "demoUser",
    roles: [
       { role: "dbOwner", db: "demo_database" }
    ]
  }
);
  • 显示 Mongo 版本
db.version();
  • DataBase 与 Collection 可以无需创建直接使用,在第一次使库或者表时,Mongo 会自动创建

  • DataBase 与 Collection 命名规范和 MySQL 一致,小写字母加下划线,结束符用 ;

  • 创建 DataBase
use demo_database;
  • 显示正在使用的 DataBase,未选择的话默认是 test
db
  • 每个 Collection 都一定有一个 _id 列,在 Spring Data Mongo 中 @Id 列就会映射到这里来,如果自己没有显示创建,那 Mongo 就会自动给你添加

  • 创建无约束的 Collection
db.createCollection("demo_collection");
  • 创建带约束的 Collection,这样的话就可以考虑使用传统的 RDBMS 了
db.createCollection("demo_collection", {
    validator: {
        $jsonSchema: {
            bsonType: "object",
            required: ["nickname", "gender", "age", "parent", "tag"],
            properties: {
                nickname: {
                    bsonType: "string",
                    description: "user nickname"
                },
                gender: {
                    enum: [0, 1, 2],
                    description: "gender for user, 0 is unknown, 1 is female, 2 is male"
                },
                age: {
                    bsonType: "int",
                    minimum: 0,
                    maximum: 150,
                    description: "user age"
                },
                parent: {
                    bsonType: "object",
                    required: ["father", "mother"],
                    properties: {
                        father: {
                            bsonType: "string",
                            description: "user's father name"
                        },
                        mather: {
                            bsonType: "string",
                            description: "user's mother name"
                        }
                    }
                },
                tag: {
                    bsonType: "array",
                    minItems: 1,
                    description: "user tag",
                    items: {
                        bsonType: "string"
                    }
                }
            }
        }
    }
});
  • 批量插入
db.getCollection("demo_collection").insertMany([
{
    nickname: "seliote",
    gender: 1,
    age: 20,
        // field 是另一个 Document
    parent: {
        father: "se",
        mother: "liote"
    },
        // field 是 Array
    tag: ["student", "teacher"]
}, {
    nickname: "seliote",
    gender: 1,
    age: 25,
    parent: {
        father: "se",
        mother: "liote"
    },
    tag: ["student"]
}]);
  • 删除一个,批量删除传入 JSONArray
db.getCollection("demo_table").deleteOne({nickname: "seliote"})
  • 查询并逆序显示
db.getCollection("demo_collection").find().sort({"nickname": -1});
  • 按照 _id 查
// 按自动生成的 _id 查
db.demo_collection.find({"_id": ObjectId("5da963f18466000044004323")});
  • 按照二级 Collection 查
db.getCollection("demo_collection").find({"parent.father": "se"});
  • 按照 Array 类型查
// 注意这里只查出全等的, tag 值为 a b c 的并不在返回范围,而且顺序也需要一致
db.demo_collection.find({tag: ["a", "b"]});
  • 查询数据量
db.getCollection("demo_collection").count();
  • 查找部分 Field
db.getCollection("demo_collection").find({}, {_id: 0, nickname: 1, gender: 1});
  • 添加索引,默认的 _id 上有索引
db.getCollection("demo_collection").createIndex({nickname: 1});
  • 查询已有索引
db.getCollection("demo_collection").getIndexes();
  • 删除当前数据库
db.dropDatabase();
  • 删除用户
db.dropUser("demo_user")
12-28 23:20