Mongo DB

239 阅读6分钟

SQL

  • SQL: Structural Query Language
  • NoSQL: Not only SQL

MongoDB 官网


使用Mongo shell

C:\Program Files\MongoDB\Server\5.0\bin>mongo

导入collection

运行以下命令导入collection

mongoimport --file C:\aircraft.json --collection aircraft --db flightmgmt --drop

mongoimport --file C:\flights.json --collection flights --db flightmgmt --drop

mongoimport --file C:\crew.json --collection crew --db flightmgmt --drop

但是遇到以下错误:

uncaught exception: SyntaxError: unexpected token: identifier : @(shell):1:24

原因是: mongodb是msi安装版, bin目录并没有mongoimport.exe工具类

解决方法: 下载工具集,文件的bin目录中所有文件, 复制到mongodb的bin目录

C:\Program Files\MongoDB\Server\5.0\bin>mongoimport --file C:\aircraft.json --collection aircraft --db flightmgmt --drop
2021-11-16T17:53:30.690+0800    connected to: mongodb://localhost/
2021-11-16T17:53:30.716+0800    dropping: flightmgmt.aircraft
2021-11-16T17:53:30.730+0800    9 document(s) imported successfully. 0 document(s) failed to import.

C:\Program Files\MongoDB\Server\5.0\bin>mongoimport --file C:\flights.json --collection flights --db flightmgmt --drop
2021-11-16T17:58:34.446+0800    connected to: mongodb://localhost/
2021-11-16T17:58:34.482+0800    dropping: flightmgmt.flights
2021-11-16T17:58:34.505+0800    10 document(s) imported successfully. 0 document(s) failed to import.

C:\Program Files\MongoDB\Server\5.0\bin>mongoimport --file C:\crew.json --collection crew --db flightmgmt --drop
2021-11-16T17:59:26.749+0800    connected to: mongodb://localhost/
2021-11-16T17:59:26.786+0800    dropping: flightmgmt.crew
2021-11-16T17:59:26.818+0800    4 document(s) imported successfully. 0 document(s) failed to import.

显式所有数据库:

> show dbs
admin       0.000GB
config      0.000GB
flightmgmt  0.000GB
local       0.000GB

当前数据库:

> db
test

切换数据库:

> use flightmgmt
switched to db flightmgmt

列出collection:

> show collections
aircraft
crew
flights

记录数:

> db.aircraft.count()
9

find()

> db.aircraft.find()
> db.aircraft.find().pretty()

查询documents(相当于一条一条的记录):

  • 默认20条
  • 要获取更多,接着输入it
  • collectionfind(query, projection): cursor方法:
    • query, 用query操作符进行过滤
    • projection, 指定要显式/隐藏的字段
    • 返回的是cursor,是符合过滤条件的documents的cursor

projection

> db.aircraft.find({},{model: 1, range: 1})
{ "_id" : ObjectId("61937f9ae8aef77db553dd4e"), "model" : "Boeing 737-800", "range" : 5765 }
{ "_id" : ObjectId("61937f9ae8aef77db553dd4f"), "model" : "Airbus A350", "range" : 15000 }
> db.aircraft.find({},{code: 0, capacity: 0, range: 0})
{ "_id" : ObjectId("61937f9ae8aef77db553dd4e"), "model" : "Boeing 737-800", "minRunwayLength" : 2500 }
{ "_id" : ObjectId("61937f9ae8aef77db553dd4f"), "model" : "Airbus A350", "minRunwayLength" : 3200 }
> db.aircraft.find({},{model: 1, _id: 0})
{ "model" : "Boeing 737-800" }
{ "model" : "Airbus A350" }
> db.flights.find({}, {duration: 1,"departure.city": 1, "destination.city": 1}).pretty()
  • projection:
    • {model: 1, range: 1}
    • 1是包括,0是不包括
    • _id是默认自动包括
    • 一般不能在一个projection里面既有包括也有不包括,只有_id 例外

cursor

//pagination
> db.aircraft.find().limit(2)
> db.aircraft.find().skip(2).limit(2)
//sort
> db.aircraft.find({}, {model: 1, _id: 0}).sort({model: 1})
{ "model" : "ATR 72" }
{ "model" : "Airbus A319" }
{ "model" : "Airbus A320" }
{ "model" : "Airbus A350" }
{ "model" : "Boeing 737-400" }

> db.aircraft.find({}, {model: 1, range: 1, _id: 0}).sort({model: 1, range: -1})
{ "model" : "ATR 72", "range" : 3218 }
{ "model" : "Airbus A319", "range" : 6900 }
{ "model" : "Airbus A320", "range" : 6000 }
{ "model" : "Airbus A350", "range" : 15000 }
{ "model" : "Boeing 737-400", "range" : 3500 }
{ "model" : "Boeing 737-800", "range" : 5765 }
{ "model" : "Boeing 737-900", "range" : 5600 }
{ "model" : "Boeing 747", "range" : 14000 }
{ "model" : "Embraer E-175", "range" : 4000 }
db.flights.find({}, {duration: 1,"departure.city": 1, "destination.city": 1}).pretty().sort({"duration": -1})
  • 也是一个object,因此可以调用一些方法
  • limit()
  • skip()
  • sort(), 1是升序,-1是降序
  • pretty()

sample 数据库

 {
	"code": "1b7ad0de-5836-489b-9791-5a81a51cdb81",
    "model" : "Boeing 737-400",
    "minRunwayLength" : 1700,
	"range": 3500,
	"capacity": 189
}
{
	"code": "eede6be6-f716-4e2e-bf81-885f0a16a50c",
    "model" : "Boeing 737-800",
    "minRunwayLength" : 2500,
	"range": 5765,
	"capacity": 200
}
{
	"code": "1b7ad0de-5836-489b-9791-5a81a51cdb81",
    "model" : "Airbus A320",
    "minRunwayLength" : 2500,
	"range": 6000,
	"capacity": 150
}
{
	"code": "a3faaef2-fe54-4949-928f-be93584da471",
    "model" : "Airbus A319",
    "minRunwayLength" : 2255,
	"range": 6900,
	"capacity": 124
}
{
	"code": "eede6be6-f716-4e2e-bf81-885f0a16a50c",
    "model" : "Boeing 737-900",
    "minRunwayLength" : 2975,
	"range": 5600,
	"capacity": 215
}
{
	"code": "51192f6b-9c26-4ef9-b843-cf241f326091",
    "model" : "Embraer E-175",
    "minRunwayLength" : 1261,
	"range": 4000,
	"capacity": 80
}
{
	"code": "00126a63-f342-4ccd-ba86-4a7beecf10c0",
    "model" : "Airbus A350",
    "minRunwayLength" : 3200,
	"range": 15000,
	"capacity": 300
}
{
	"code": "4f356f56-84dd-484f-a5f7-b960dfba5823",
    "model" : "Boeing 747",
    "minRunwayLength" : 3100,
	"range": 14000,
	"capacity": 467
}
{
	"code": "0c3a60d6-8c99-472e-bf23-c1e689c5f6eb",
    "model" : "ATR 72",
    "minRunwayLength" : 1000,
	"range": 3218,
	"capacity": 78
}
{
    "type" : "International",
    "delayed" : false,
    "duration" : 150,
	"departureDate":{"$date": "2020-02-20T21:30:00Z"},
	"distanceKm": 1870,
    "departure" : {
        "code": "CDG",
        "city": "Paris",
        "country":"France",
        "runwayLength": 4215,
		"location": {
			"type": "Point",
			"coordinates": [2.3, 48.8]
		}
    },
    "destination" : {
        "code": "OTP",
        "city": "Bucharest",
        "country":"Romania",
        "runwayLength": 3500,
		"location": {
			"type": "Point",
			"coordinates": [26.1, 44.4]
		}
    },
    "aircraftCode" : "1b7ad0de-5836-489b-9791-5a81a51cdb81",
    "crew":[
        {"name": "Renaud Cahun", "position": "Captain", "hoursSlept": 8},
        {"name": "Nina Peltier", "position": "Attendant", "nationality": "France", "hoursSlept": 6},
		{"name": "Lou Galopin", "position": "Attendant", "nationality": "France", "hoursSlept": 7}
    ]
}
{
    "type" : "International",
    "delayed" : false,
    "duration" : 120,
	"departureDate":{"$date": "2020-02-20T21:20:00Z"},
	"distanceKm": 1730,
    "departure" : {
        "code": "CDG",
        "city": "Paris",
        "country":"France",
        "runwayLength": 4215,
		"location": {
			"type": "Point",
			"coordinates": [2.3, 48.8]
		}
    },
    "destination" : {
        "code": "LIS",
        "city": "Lisabon",
        "country":"Portugal",
        "runwayLength": 3805,
		"location": {
			"type": "Point",
			"coordinates": [-9.1, 38.7]
		}
    },
    "aircraftCode" : "eede6be6-f716-4e2e-bf81-885f0a16a50c",
    "crew":[
        {"name": "Pierre Cotard", "position": "Captain", "nationality": "France", "hoursSlept": 4},
        {"name": "Amanda Lucas", "position": "Attendant", "hoursSlept": 6},
        {"name": "Joe Stan", "position": "Attendant", "nationality": "UK", "hoursSlept": 8}
    ]
}
{
    "type" : "International",
    "delayed" : true,
    "duration" : 260,
	"departureDate":{"$date": "2020-02-20T23:00:00Z"},
	"distanceKm": 3200,
    "departure" : {
        "code": "IST",
        "city": "Istanbul",
        "country":"Turkey",
        "runwayLength": 4100,
		"location": {
			"type": "Point",
			"coordinates": [28.9, 41.0]
		}
    },
    "destination" : {
        "code": "LIS",
        "city": "Lisabon",
        "country":"Portugal",
        "runwayLength": 3805,
		"location": {
			"type": "Point",
			"coordinates": [-9.1, 38.7]
		}
    },
    "aircraftCode" : "1b7ad0de-5836-489b-9791-5a81a51cdb81",
    "crew":[
        {"name": "Hakan Antal", "position": "Captain", "nationality": "Turkey", "hoursSlept": 5},
        {"name": "Anna Hassan", "position": "Attendant", "hoursSlept": 4},
        {"name": "Adina Popescu", "position": "Attendant", "hoursSlept": 9}
    ]
}
{
    "type" : "Internal",
    "delayed" : true,
    "duration" : 80,
	"departureDate":{"$date": "2020-02-20T17:15:00Z"},
	"distanceKm": 400,
    "departure" : {
        "code": "CDG",
        "city": "Paris",
        "country":"France",
        "runwayLength": 4215,
		"location": {
			"type": "Point",
			"coordinates": [2.3, 48.8]
		}
    },
    "destination" : {
        "code": "LYS",
        "city": "Lyon",
        "country":"France",
        "runwayLength": 4000,
		"location": {
			"type": "Point",
			"coordinates": [4.8, 45.7]
		}
    },
    "aircraftCode" : "51192f6b-9c26-4ef9-b843-cf241f326091",
    "crew":[]
}
{
    "type" : "Internal",
    "delayed" : false,
    "duration" : 65,
	"departureDate":{"$date": "2020-02-21T15:45:00Z"},
	"distanceKm": 470,
    "departure" : {
        "code": "LYS",
        "city": "Lyon",
        "country":"France",
        "runwayLength": 4000,
		"location": {
			"type": "Point",
			"coordinates": [4.8, 45.7]
		}
    },
    "destination" : {
        "code": "NIC",
        "city": "Nice",
        "country":"France",
        "runwayLength": 2570,
		"location": {
			"type": "Point",
			"coordinates": [7.2, 43.7]
		}
    },
    "aircraftCode" : "a3faaef2-fe54-4949-928f-be93584da471",
    "crew":[
        {"name": "Patricia Mosar", "position": "Attendant", "hoursSlept": 7},
        {"name": "Jerome Trudeau", "position": "Attendant", "nationality": "France", "hoursSlept": 4}
    ]
}
{
    "type" : "Internal",
    "delayed" : false,
    "duration" : 100,
	"departureDate":{"$date": "2020-03-21T11:10:00Z"},
	"distanceKm": 419,
    "departure" : {
        "code": "MUC",
        "city": "Munich",
        "country":"Germany",
        "runwayLength": 4000,
		"location": {
			"type": "Point",
			"coordinates": [11.7, 48.3]
		}
    },
    "destination" : {
        "code": "FRA",
        "city": "Frankfurt",
        "country":"Germany",
        "runwayLength": 4000,
		"location": {
			"type": "Point",
			"coordinates": [8.56, 50.0]
		}
    },
    "crew":[
        {"name": "Engel Wagner", "position": "Captain", "hoursSlept": 2},
        {"name": "Louise Baier", "position": "Attendant", "nationality": "Germany", "hoursSlept": 7}
    ]
}
{
    "type" : "International",
    "delayed" : false,
    "duration" : 110,
	"departureDate":{"$date": "2020-03-22T14:10:00Z"},
	"distanceKm": 800,
    "departure" : {
        "code": "MUC",
        "city": "Munich",
        "country":"Germany",
        "runwayLength": 4000,
		"location": {
			"type": "Point",
			"coordinates": [11.7, 48.3]
		}
    },
    "destination" : {
        "code": "NIC",
        "city": "Nice",
        "country":"France",
        "runwayLength": 2570,
		"location": {
			"type": "Point",
			"coordinates": [7.2, 43.7]
		}
    },
	"aircraftCode" : null,
    "crew":[
    ]
}
{
    "type" : "Intercontinental",
    "delayed" : false,
    "duration" : 600,
	"departureDate":{"$date": "2020-03-20T18:10:00Z"},
	"distanceKm": 6400,
    "departure" : {
        "code": "MUC",
        "city": "Munich",
        "country":"Germany",
        "runwayLength": 4000,
		"location": {
			"type": "Point",
			"coordinates": [11.7, 48.3]
		}
    },
    "destination" : {
        "code": "JFK",
        "city": "New York",
        "country":"USA",
        "runwayLength": 4423,
		"location": {
			"type": "Point",
			"coordinates": [-74, 40.7]
		}
    },
	"aircraftCode" : "00126a63-f342-4ccd-ba86-4a7beecf10c0",
    "crew":[
		{"name": "Marcel Danzig", "position": "Captain", "hoursSlept": 7},
        {"name": "Gotthard Merz", "position": "Attendant", "nationality": "Germany", "hoursSlept": 6},
		{"name": "Bastian Steyer", "position": "Attendant", "nationality": "Germany", "hoursSlept": 3},
		{"name": "Isabell Jahnke", "position": "Attendant", "nationality": "Germany", "hoursSlept": 5}
    ]
}
{
    "type" : "Intercontinental",
    "delayed" : true,
    "duration" : 645,
	"departureDate":{"$date": "2020-03-20T9:20:00Z"},
	"distanceKm": 5700,
    "departure" : {
        "code": "JFK",
        "city": "New York",
        "country":"USA",
        "runwayLength": 4423,
		"location": {
			"type": "Point",
			"coordinates": [-74, 40.7]
		}
    },
    "destination" : {
        "code": "CDG",
        "city": "Paris",
        "country":"France",
        "runwayLength": 4215,
		"location": {
			"type": "Point",
			"coordinates": [2.3, 48.8]
		}
    },
	"aircraftCode" : "4f356f56-84dd-484f-a5f7-b960dfba5823",
    "crew":[
        {"name": "Ernest Boucher", "position": "Attendant", "nationality": "France", "hoursSlept": 6},
		{"name": "Amadou Chevalier", "position": "Attendant", "hoursSlept": 7},
		{"name": "Leslie Nee", "position": "Attendant", "nationality": "France", "hoursSlept": 8}
    ]
}
{
    "type" : "International",
    "delayed" : false,
    "duration" : 120,
	"departureDate":{"$date": "2020-02-22T20:00:00Z"},
	"distanceKm": 1504,
    "departure" : {
        "code": "OTP",
        "city": "Bucharest",
        "country":"Romania",
        "runwayLength": 3500,
		"location": {
			"type": "Point",
			"coordinates": [26.1, 44.4]
		}
    },
    "destination" : {
        "code": "NIC",
        "city": "Nice",
        "country":"France",
        "runwayLength": 2570,
		"location": {
			"type": "Point",
			"coordinates": [7.2, 43.7]
		}
    },
    "crew":[
        {"name": "Popescu Ion", "position": "Captain", "hoursSlept": 8}
    ]
}
{
	"name": "Andrei Luca",
	"skills": ["technical", "management"],
	"address": {"city": "Bucharest", "country": "Romania"}
}
{
	"name": "Anna Smith",
	"skills": ["technical", "management"],
	"address": {"city": "Bucharest", "country": "Romania"}
}
{
	"name": "Francois Picard",
	"skills": [],
	"address": {"city": "Paris", "country": "France"}
}
{
	"name": "Gunter Hoff",
	"skills": ["engineering"],
	"address": {"city": "Berlin", "country": "Germany"}
}

工具集

  • bsondump - display BSON files in a human-readable format
  • mongoimport - Convert data from JSON, TSV or CSV and insert them into a collection
  • mongoexport - Write an existing collection to CSV or JSON format
  • mongodump/mongorestore - Dump MongoDB backups to disk in .BSON format, or restore them to a live database
  • mongostat - Monitor live MongoDB servers, replica sets, or sharded clusters
  • mongofiles - Read, write, delete, or update files in GridFS
  • mongotop - Monitor read/write activity on a mongo server