How we want to delete a collection can take the db.test.drop () Way, in fact, behind this, this function is running the drop command. The same effect can be achieved with RunCommand.
Let's start by creating a new collection to use as a test:
> Use Maple1
Switched to DB Maple1
> Db.maple1.insert ({' name ': ' Zhanghongfeng '})
Writeresult ({"ninserted": 1})
Delete this collection, the results of the feedback ok:1 represents the Success
> Db.runcommand ({"Drop": "Maple1"})
{"ns": "Maple1.maple1", "Nindexeswas": 1, "OK": 1}
If the operation fails, there will be a reason why the errmsg prompt failed .
> Db.runcommand ({"Drop": "Maple1"})
{
"OK": 0,
"ErrMsg": "NS not Found",
"Code": 26,
"codename": "Namespacenotfound"
}
If you want to see all the commands running db.listcommands () in the Shell , you can get all the commands. Below is a list of the most common commands for MONGODB.
1 Buildinfo: returns The version number of the MONGODB server and the host's operating system
> Db.runcommand ({"Buildinfo": 1})
{
"Version": "3.4.7",
"Gitversion": "CF38C1B8A0A8DCA4A11737581BEAFEF4FE120BCD",
"Modules": [],
"Allocator": "Tcmalloc",
"Javascriptengine": "Mozjs",
"SysInfo": "Deprecated",
"Versionarray": [
3,
4,
7,
0
],
"OpenSSL": {
"Running": "OpenSSL 1.0.2g 1 Mar 2016",
"Compiled": "OpenSSL 1.0.2g 1 Mar 2016"
},
"Buildenvironment": {
"Distmod": "",
"Distarch": "x86_64",
"CC": "CC:CC (Ubuntu 7.1.0-13ubuntu1) 7.1.0",
"Ccflags": "-fno-omit-frame-pointer-fno-strict-aliasing-ggdb-pthread-wall-wsign-compare-wno-unknown-pragmas- Wno-nonnull-compare-wno-error=c++1z-compat-wno-error=noexcept-type-wno-error=format-truncation-wno-error= Int-in-bool-context-wno-overflow-winvalid-pch-werror-o2-wno-unused-local-typedefs-wno-unused-function- Wno-deprecated-declarations-wno-unused-const-variable-wno-unused-but-set-variable-wno-missing-braces- FSTACK-PROTECTOR-STRONG-FNO-BUILTIN-MEMCMP ",
"Cxx": "g++: g++ (Ubuntu 7.1.0-13ubuntu1) 7.1.0",
"Cxxflags": "-g-o2-fdebug-prefix-map=/build/mongodb-5gk9fl/mongodb-3.4.7=." -fstack-protector-strong-wformat-werror=format-security-woverloaded-virtual-wno-maybe-uninitialized-std=c++11 " ,
"Linkflags": "-wl,-bsymbolic-functions-wl,-z,relro-pthread-wl,-z,now-rdynamic-wl,--fatal-warnings- FSTACK-PROTECTOR-STRONG-FUSE-LD=GOLD-WL,--build-id-wl,-z,noexecstack-wl,--Warn-execstack-wl,-z,relro ",
"Target_arch": "x86_64",
"Target_os": "Linux"
},
"Bits": 64,
"Debug": false,
"Maxbsonobjectsize": 16777216,
"Storageengines": [
"Devnull",
"Ephemeralfortest",
"Mmapv1",
"Wiredtiger"
],
"OK": 1
}
2 Collstats: returns statistics for the specified collection, including data size, allocated storage space, and index size
> Db.runcommand ({"Collstats": "Maple"})
{
"NS": "Maple.maple",
"Size": 234,
"Count": 4,
"Avgobjsize": 58,
"Storagesize": 36864,
"Capped": false,
"Wiredtiger": {
"Metadata": {
"FormatVersion": 1
},
3 DISTINCT: lists all the different values for the specified key of the document in the specified collection that satisfy the query criteria
> Db.runcommand ({"distinct": "Maple", "key": "Name"})
{"Values": ["Zhanghongfeng_maple"], "OK": 1}
4 Drop and dropdatabase: Delete all data from the collection and all data for the current database
5 GetLastError: View the error message or other status information for the last operation performed on this collection.
6 IsMaster: Check whether this server is the primary or the server
> Db.runcommand ({"IsMaster": 1})
{
"IsMaster": true,
"Maxbsonobjectsize": 16777216,
"Maxmessagesizebytes": 48000000,
"Maxwritebatchsize": 1000,
"LocalTime": Isodate ("2017-12-27t13:45:21.228z"),
"Maxwireversion": 5,
"Minwireversion": 0,
"ReadOnly": false,
"OK": 1
}
7 Listcommands: Returns all commands that can be run on the server
8 listdatabases: list all databases on the server
9 Ping: detects if the link to the server is normal
> Db.runcommand ({"Ping": 1})
{"OK": 1}
10
Fixed collection:
As the name implies, a fixed collection is a collection that is able to fix the size of the collection and the number of documents. The fixed collection is created using createcollection. Size, as shown below , represents the sizes of the collection, andMax represents the maximum number of documents
> db.createcollection ("Fixedcollection", {capped:true,size:1000,max:2})
{"OK": 1}
Insert 2 documents
>db.fixedcollection.insert ({"Name": "Zhanghongfeng", "Age": 33})
> Db.fixedcollection.insert ({"Name": "Maple", "Age": 32})
Show 2 documents inserted
> Db.fixedcollection.find ()
{"_id": ObjectId ("5a43a73ba889535e61dc29a8"), "name": "Zhanghongfeng", "Age": 33}
{"_id": ObjectId ("5a43a77ba889535e61dc29a9"), "name": "Maple", "Age": 32}
Insert a document again
> Db.fixedcollection.insert ({"Name": "ZHF", "Age": 32})
Writeresult ({"ninserted": 1})
By querying, you can see that the oldest inserted document, Name:zhanghongfeng , was replaced when a document was inserted. This is because we specify a maximum number of 2 documents in a fixed collection . As a result, the newly inserted document replaces the previous document, replacing the earliest established document with the replacement method.
> Db.fixedcollection.find ()
{"_id": ObjectId ("5a43a77ba889535e61dc29a9"), "name": "Maple", "Age": 32}
{"_id": ObjectId ("5a43a78ca889535e61dc29aa"), "name": "ZHF", "Age": 32}
Fixed collections are arranged in the same way as ring queues, by specifying the order in which queries are found , $natural: -1 follows the document's insertion time from the early order of arrival, that is, starting from the end of the queue. $natural: 1 is starting from the head of the queue
> Db.fixedcollection.find (). Sort ({"$natural":-1})
{"_id": ObjectId ("5a43a78ca889535e61dc29aa"), "name": "ZHF", "Age": 32}
{"_id": ObjectId ("5a43a77ba889535e61dc29a9"), "name": "Maple", "Age": 32}
> Db.fixedcollection.find (). Sort ({"$natural": 1})
{"_id": ObjectId ("5a43a77ba889535e61dc29a9"), "name": "Maple", "Age": 32}
{"_id": ObjectId ("5a43a78ca889535e61dc29aa"), "name": "ZHF", "Age": 32}
MongoDB Learning: Database commands and fixed collections