mongodb的命令:

 

启动服务

./bin/mongod -f ./conf/mongod.conf

启动 命令行

./bin/mongo localhost:12345

> db
test
> use local
switched to db local
> db.createCollection("mycoll", {capped:true, size:100000})
{
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(0, 0),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(0, 0)
}

 初始化:

rs.initiate()
{
	"info2" : "no configuration specified. Using a default configuration for the set",
	"me" : "localhost:12345",
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1599893065, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1599893065, 1)
}

数据库

查看数据库

rs0:SECONDARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
db.col.findOne()
null

 创建一个db:’

rs0:PRIMARY> use jinghang
switched to db jinghang
rs0:PRIMARY> db
jinghang
rs0:PRIMARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
rs0:PRIMARY> db.jinghang.insert({"name":"景航教程"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> show dbs
admin     0.000GB
config    0.000GB
jinghang  0.000GB
local     0.000GB

删除db:

rs0:PRIMARY> show dbs
admin     0.000GB
config    0.000GB
jinghang  0.000GB
local     0.000GB
rs0:PRIMARY> use jinghang
switched to db jinghang
rs0:PRIMARY> db.dropDatabase()
{
	"dropped" : "jinghang",
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1599901507, 2),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1599901507, 2)
}
rs0:PRIMARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB

创建集合:

rs0:PRIMARY> show dbs
admin     0.000GB
config    0.000GB
jinghang  0.000GB
local     0.000GB
rs0:PRIMARY>  use test
switched to db test
rs0:PRIMARY> db.createCollection("jinghang")
{
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1599901881, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1599901881, 1)
}
rs0:PRIMARY> show collections
jinghang
rs0:PRIMARY> db.createCollection("mycol", { capped : true, autoIndexId : true, size : 
...    6142800, max : 10000 } )
{
	"note" : "The autoIndexId option is deprecated and will be removed in a future release",
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1599901909, 2),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1599901909, 2)
}
rs0:PRIMARY> db.mycol2.insert({"name" : "景航教程"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> show collections
jinghang
mycol
mycol2
rs0:PRIMARY> 

删除集合:

rs0:PRIMARY> use mydb
switched to db mydb
rs0:PRIMARY> show collections
rs0:PRIMARY> db.mycol2.insert({"name" : "景航教程"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> db.mycol3.insert({"name" : "景航教程"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> db.mycol4.insert({"name" : "景航教程"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> show collections
mycol2
mycol3
mycol4
rs0:PRIMARY> db.mycol2.drop()
true
rs0:PRIMARY> show collections
mycol3
mycol4
rs0:PRIMARY> 

插入文档:

rs0:PRIMARY> db.col.insert({title: 'MongoDB 教程', 
...     description: 'MongoDB 是一个 Nosql 数据库',
...     by: '景航教程',
...     url: 'http://www.jinghang.com',
...     tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100
... })
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> show collections
col
mycol3
mycol4
rs0:PRIMARY> db.col.find()
{ "_id" : ObjectId("5f5c92e3da33b31d5e0f590b"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "景航教程", "url" : "http://www.jinghang.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
rs0:PRIMARY> db.col.portI().find()
uncaught exception: TypeError: db.col.portI is not a function :
@(shell):1:1
rs0:PRIMARY> document=({title: 'MongoDB 教程', 
...     description: 'MongoDB 是一个 Nosql 数据库',
...     by: '景航教程',
...     url: 'http://www.jinghang.com',
...     tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100});
{
	"title" : "MongoDB 教程",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
rs0:PRIMARY> db.col.insert(document)
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> 

更新文档:

rs0:PRIMARY> db.col.insert({
...     title: 'MongoDB 教程', 
...     description: 'MongoDB 是一个 Nosql 数据库',
...     by: '景航教程',
...     url: 'http://www.jinghang.com',
...     tags: ['mongodb', 'database', 'NoSQL'],
... likes: 100})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB教'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
rs0:PRIMARY> db.col.find().pretty()
{
	"_id" : ObjectId("5f5c92e3da33b31d5e0f590b"),
	"title" : "MongoDB教",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
{
	"_id" : ObjectId("5f5c9340da33b31d5e0f590c"),
	"title" : "MongoDB 教程",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
{
	"_id" : ObjectId("5f5c93c6da33b31d5e0f590d"),
	"title" : "MongoDB 教程",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
rs0:PRIMARY> db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB 程'}},{multi:true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
rs0:PRIMARY> db.col.find().pretty()
{
	"_id" : ObjectId("5f5c92e3da33b31d5e0f590b"),
	"title" : "MongoDB教",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
{
	"_id" : ObjectId("5f5c9340da33b31d5e0f590c"),
	"title" : "MongoDB 程",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
{
	"_id" : ObjectId("5f5c93c6da33b31d5e0f590d"),
	"title" : "MongoDB 程",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
rs0:PRIMARY>

替换:

rs0:PRIMARY> db.col.save({
...     "_id" : ObjectId("56064f89ade2f21f36b03136"),
...     "title" : "MongoDB",
...     "description" : "MongoDB 是一个 Nosql 数据库",
...     "by" : "jinghang",
...     "url" : "http://www.jinghang.com",
...     "tags" : [
...             "mongodb",
...             "NoSQL"
...     ],
... "likes" : 110})
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : ObjectId("56064f89ade2f21f36b03136")
})
rs0:PRIMARY> db.col.find().pretty()
 [
                "mongodb",
                "NoSQL"
        ],
        "likes" : 110}{
	"_id" : ObjectId("5f5c92e3da33b31d5e0f590b"),
	"title" : "MongoDB教",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
{
	"_id" : ObjectId("5f5c9340da33b31d5e0f590c"),
	"title" : "MongoDB 程",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
{
	"_id" : ObjectId("5f5c93c6da33b31d5e0f590d"),
	"title" : "MongoDB 程",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
{
	"_id" : ObjectId("56064f89ade2f21f36b03136"),
	"title" : "MongoDB",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "jinghang",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"NoSQL"
	],
	"likes" : 110
}
rs0:PRIMARY> 
rs0:PRIMARY> {
...         "_id" : ObjectId("56064f89ade2f21f36b03136"),
...         "title" : "MongoDB",
...         "description" : "MongoDB 是一个 Nosql 数据库",
...         "by" : "jinghang",
...         "url" : "http://www.jinghang.com",
...         "tags" : [
...                 "mongodb",
...                 "NoSQL"
...         ],
...         "likes" : 110}
uncaught exception: SyntaxError: unexpected token: ':' :
@(shell):2:14
rs0:PRIMARY> db.col.find().pretty()
{
	"_id" : ObjectId("5f5c92e3da33b31d5e0f590b"),
	"title" : "MongoDB教",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
{
	"_id" : ObjectId("5f5c9340da33b31d5e0f590c"),
	"title" : "MongoDB 程",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
{
	"_id" : ObjectId("5f5c93c6da33b31d5e0f590d"),
	"title" : "MongoDB 程",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "景航教程",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"database",
		"NoSQL"
	],
	"likes" : 100
}
{
	"_id" : ObjectId("56064f89ade2f21f36b03136"),
	"title" : "MongoDB",
	"description" : "MongoDB 是一个 Nosql 数据库",
	"by" : "jinghang",
	"url" : "http://www.jinghang.com",
	"tags" : [
		"mongodb",
		"NoSQL"
	],
	"likes" : 110
}
rs0:PRIMARY> 

 

你可能感兴趣的:(mongodb)