Introduction to MongoDB Rookie (1): Concepts and Additions, Deletions, Revisions, and Indexing

Posted by Michael Lasky on Sat, 08 Jun 2019 01:15:02 +0200

I. Start-up

Specify - dbpath as the storage folder and start with the default port

mongod.exe --dbpath c:\data\db

2. windows Adding Services

To set mongodb as a windows service

mongod.exe --bind_ip yourIPadress --logpath "C:\data\dbConf\mongodb.log" --logappend --dbpath "C:\data\db" --port yourPortNumber --serviceName "YourServiceName" --serviceDisplayName "YourServiceName" --install
  • - bind_ip: Binding service IP, if binding 127.0.0.1, can only be accessed locally without specifying the default local all IP
  • logpath: Define the MongoDB log file, note that the specified file is not a directory
  • logappend: Write logs in an additional way
  • dbpath: Specify the database path
  • Port: Specify the service port number, default port 27017
  • - serviceName: Specify the name of the service
  • Service DisplayName: Specify the name of the service and execute when there are multiple mongodb services.
  • install: Specify installation as a Windows service.

concept

  • Database: database
  • collection: database tables / collections
  • Document: Data record row/document
  • Field: Data field/field
  • index: index
  • Primary key: primary key, and MongoDB automatically sets the _id field as primary key
  • ps: Table connection, not supported by MongoDB
  • Documents: MongoDB documents do not need to set the same fields, and the same fields do not need the same data type. The same field can be stored in strings, values, etc. In the same set (table), the number of domains (fields) of each document (record) can vary.
  • Collection: Collection is the MongoDB document group. Collection exists in the database. Collection has no fixed structure. This means that you can insert different formats and types of data into the collection, but usually the data we insert into the collection will have some relevance. When the first document is inserted, the collection is created.

III. Operating databases

//Display the database currently in use
db
//Display all databases
show dbs
//Create a database and choose to connect to a specified database (only show dbs are displayed after inserting the first record)
use testDB
//Delete the currently selected database
db.dropDatabase()

IV. Operational Set

//Display all collections under the current library
show collections
db.collections()
//Create capped collections, which cannot be deleted, can only be updated, and the size cannot be changed
db.createCollection("myco", {capped:true, size:10000})
//Delete collections
db.tb.drop()

5. Insert Documents

db.runoob.insert({"x":10})
//Insert a single bar
db.runoob.insertOne({"x":10})
//Insert multiple strips
db.runoob.insertMany([{"x":10},{"x":11}])

You can also insert documents using the db.col.save(document) command. If you do not specify the save() method for the _id field, it is similar to the insert() method. If the _id field is specified, the data for that _id is updated.

6. Delete Documents

//Delete all
db.tb_a.remove({"title":"x"})
//Delete a single entry
db.tb_a.remove({"title":"hello"},{justOne:true});
db.tb_a.remove({"title":"hello"},1);
  • query: (optional) The condition for deleting a document.
  • justOne: (optional) If set to true or 1, only one document is deleted.
  • Write Concern: (optional) The level at which an exception is thrown.

Updating Documents

//Set the content field of a document whose collection tb_a meets the criteria of title="hihi" to aaaa
db.tb_a.update({"title":"hihi"},{$set:{"content":"aaaa"}})
//Insert an entry when the document does not exist in the condition
db.tb_a.update({"title":"x"},{$set:{"content":"aaaaaaaaaaaaaaaaa"}},{upsert:true});
db.tb_a.update({"title":"x"},{$set:{"content":"aaaaaaaaaaaaaaaaa"}},true,false);
//Update multiple documents when there are multiple documents in a condition
db.tb_a.update({"title":"hi"},{$set:{"content":"abcdefg"}},{multi:true});
db.tb_a.update({"title":"hi"},{$set:{"content":"abcdefg"}},false,true);
  • Query: The query condition of update is similar to that of where in sql update query.
  • Update: update objects and some updated operators (e.g., inc...) It can also be understood as following the set in the sql update query
  • upsert: Optional. This parameter means whether to insert objNew if there is no update record, true is insert, false by default, and no insert.
  • multi: Optionally, mongodb defaults to false, updates only the first record found, and if this parameter is true, updates all the records found on condition.
  • Write Concern: Optional, throw exception level.

8. Finding Documents

//View all documents in tb_a collection
db.tb_a.find()
//Search by condition
db.tb_a.find({"title":"hello"})
//Conditional lookup, documentation (json) view
db.tb_a.find({"title":"hello"}).pretty()
//Find by condition, return one
db.tb_a.findOne({"title":"hello"})
//Restrict the number of returned documents with limit
db.tb_b.find().pretty().limit(2)
//Skp is used to skip the number of document bars. Note: The default parameter of skip() method is 0 . 
db.tb_b.find().pretty().limit(2).skip(2)
//Using sort sort sort,1Ascending order,-1Descending order
db.tb_b.find().pretty().sort({"content":1})

Use conditions gt (greater than), gte (greater than or equal to), lt (less than), lte (less than or equal to), and $ne (not equal to) lookup

//greater than5
db.tb_b.find({"content":{$gt:5}}).pretty()
//Greater than or equal to5
db.tb_b.find({"content":{$gte:5}}).pretty()
//less than5
db.tb_b.find({"content":{$lt:5}}).pretty()
//Less than or equal to5
db.tb_b.find({"content":{$lte:5}}).pretty()
//Not equal to5
db.tb_b.find({"content":{$nt:5}}).pretty()

AND Conditions

db.tb_b.find({"content":5,"title":"hi"}).pretty()

OR condition

db.tb_b.find({$or:[{"content":1},{"content":9}]}).pretty()

AND+OR Combination Conditions

db.tb_b.find({"title":"hello",$or:[{"content":5},{"content":9}]}).pretty()

Type matching $type
Type list is available for viewing http://www.runoob.com/mongodb/mongodb-operators-type.html

db.tb_b.find({"content":{$type:2}}).pretty()

IX. Creating Index

1 in ascending order and - 1 in descending order

//Single field
db.col.ensureIndex({"title":1})
//Multi-field
db.col.ensureIndex({"title":1,"content":-1})
//Perform index creation in the background
db.values.ensureIndex({"title":1,"content":-1}, {background: true})

Define data as a variable

This variable can be inserted directly

document=({title: 'MongoDB', 
    content: 'MongoDB Is a Nosql data base'
});

Topics: MongoDB Database less Windows