Q. SQL vs MongoDB 比較
SQL MongoDB
database database
table collection
row document
column field

More Info : MongoDB 与 MySQL 深入对比

Q. Windows 如何啓動mongodb?

mongod --storageEngine=mmapv1 --dbpath [your-path]

mongod --storageEngine=mmapv1 --dbpath D:\MongoDB\mydb

mongod --storageEngine=mmapv1 --dbpath D:\Misc\MongoDB\Data\mydb

Q. 如何使用mongo shell ?

mongo

mongo --version 查詢目前mongo版本

Q. MongoDB Shell Commands
Command Description
db.stat() show db info
show dbs show existing databases
show collections show collections in current database
use [database] switch to db [database]
db.[collection].insert([document]) insert a document into a collection, MongoDB willcreate the [collection] if it doesn’t exist
db.[collection].drop() delete collection
db.[collection].find() query all documents in collection
db.[collection].find([condition]) query documents have specific field value
TODO: 條件運算子

ex:

db.employee.insert({"name":"welen","country":"tw", "age":"35"})

db.employee.find({"age":"35"})

Q. MongoDB $regex Query Operators

Ans. $regex provides regular expression capabilities for pattern matching strings in queries

Syntax

{ <field>: { $regex: /pattern/, $options: '<options>' } }

{ <field>: { $regex: /pattern/<options> } }

^ for the start

$ for the end

.* for unspecific string

options

i for case insensitively

ex:

match all documents where name field contains w

db.employee.find({name: {$regex: /w/}})

match all documents where name field end with n

db.employee.find({name: {$regex: /n$/}})

match all documents where name field start with 8

db.employee.find({name: {$regex: /^8/}})

match all documents contains m, follows by a string with line at end

db.products.find( { description: { $regex: /m.*line/, $options: 'si' } } )

results matching ""

    No results matching ""