//1、安装MongoDB数据库
cd D:SoftwareMongoDBdatain
mongod --dbpath "D:SoftwareMongoDBdatadb" --logpath "D:SoftwareMongoDBdatalogMongoDB.log" --install --serviceName "MongoDB"
mongod.exe --dbpath "D:SoftwareMongoDBdatadb" --logpath "D:SoftwareMongoDBdatalogMongo.log" --install --serviceName "MongoDB" --logappend --directoryperdb
mongod.exe --dbpath="D:SoftwareMongoDBdatadb" --logpath="D:SoftwareMongoDBdatalogMongoLog.log" --install --serviceName "MongoDB" --logappend --directoryperdb
net start MongoDB
net sopt MongoDB
//2、插入单个文本
db.goodsbaseinf.insert(
{
name:"C语言",
bookprice:33.2,
adddate:2019-9-1,
allow:true,
baseinf:{
ISBN:1111111111,press:"清华大学出版社"
},
tags:["good","book","it","Program"]
}
)
//3、插入多行文本
db.goodsbaseinf.insert(
[
{
intem:"小学生教材",name:"小学一年级语文上册",price:12
},
{
intem:"中学生教材",name:"初一一年级语文上册",price:12
},
{
intem:"高中生教材",name:"高中一年级语文上册",price:12
},
{
intem:"大学生教材",name:"大学一年级语文上册",price:12
}
]
)
//4、用变量名插入文档
document = ({name:"MongoDB语言",price:44})
db.goodsbaseinf.insert(document)
//5、有序插入多条文档
db.goodsbaseinf.insert(
[
{
_id:10, intem:"小学生教材",name:"小学一年级语文上册",price:100
},
{
_id:11, intem:"小学生教材",name:"小学二年级语文上册",price:200
},
{
_id:12, intem:"小学生教材",name:"小学三年级语文上册",price:300
},
{ordered:true}
]
)
//6、自定义写出错确认级别(含insert命令出错返回对象显示)
db.goodsbaseinf.insert(
{_id:1,item:"大学生教材",name:"大学英语上册",price:90},
{writeContent:{w:"maiority",wtimeout:5000}} //5s
)
//7、单条插入命令
db.goodsbaseinf.insertOne(
{name:"Jave语言编程",price:78
}
)
//8、多条插入命令
db.goodsbaseinf.insert(
[
{name:".NET语言编程",price:11},
{name:"PHP语言编程",price:22},
{name:"C++语言编程",price:33}
]
)
//9、查询集合所有文档
db.goodsbaseinf.find()
db.goodsbaseinf.find().pretty() //格式化显示
//10、等价条件查询
db.goodsbaseinf.find(
{
name: "C语言编程"
}
)
//11、等价条件查询:按字段显示
db.goodsbaseinf.find(
{
name: "C语言编程"
},
{name:1,price:1,_id:0 } //1 显示,0 不显示
)
//12、嵌套文档查询
db.goodsbaseinf.find(
{
"baseinf.press":"清华大学出版社" //key值是双引号
}
)
//13、数组查询
db.goodsbaseinf.find(
{
tags:["good","book","it","Program"] //必须大小写
}
)
db.goodsbaseinf.find(
{
tags: "good" //查询某一个值
}
)
db.goodsbaseinf.find(
{
tags: {$size:4} //查询4个元素的数组
}
)
//14、查找null值字段,查找指定无值字段
db.goodsbaseinf.insert(
[
{_id:2222,toy:null},
{_id:1112}
]
)
//15、查找null值字段
db.goodsbaseinf.find(
{_id:2222,toy:null}
)
//16、查找不存在值
db.goodsbaseinf.find(
{_id:1112,toy:{$exists:false}}
)
//17、查找返回值游标操作
var showCursor=db.goodsbaseinf.find()
showCursor.forEach(printjson); //打印显示游标获取的集合
//18、limit 与 skip 方法查询
db.goodsbaseinf.find().limit(1) //返回第一条文档
db.goodsbaseinf.find().skip(2) //返回第3条开始之后的文档
//19、$in运算符 相等于或( or)
db.goodsbaseinf.find(
{
_id:{ $in:[12, 11] }
}
)
//20、查询区间条件
db.goodsbaseinf.find(
{
price:{$gt:3 ,$lt : 340}
}
)