MongoDB下载安装测试及使用
1.下载安装 64位:mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi http://downloads.mongodb.com/win32/mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi?_ga=1.238525191.607472782.1411452026 32位:mongodb-win32-i386-2.6.5
1.下载安装
64位:mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi
http://downloads.mongodb.com/win32/mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi?_ga=1.238525191.607472782.1411452026
32位:mongodb-win32-i386-2.6.5.zip
https://fastdl.mongodb.org/win32/mongodb-win32-i386-2.6.5.zip?_ga=1.181732967.1708362836.1411364634
2.安装目录:
将应用安装到此目录下面:
D:\MongoDB\
3.新建目录
D:\MongoDB\data\db
D:\MongoDB\data\log
4.启动进城:
cd D:\MongoDB\bin
mongod -dbpath “D:\MongoDB\data\db”
5.测试连接
新开一个cmd窗口,进入MongoDB的bin目录,输入mongo或者mongo.exe,出现如下信息说明测试通过,此时我们已经进入了test这个数据库,如何进入其他数据库下面会说。
输入exit或者ctrl+C可退出。
6.当mongod.exe被关闭时,mongo.exe 就无法连接到数据库了,因此每次想使用mongodb数据库都要开启mongod.exe程序,所以比较麻烦,此时我们可以将MongoDB安装为windows服务
还是运行cmd,进入bin文件夹,执行下列命令
控制台执行命令:D:\MongoDB\bin>
mongod --dbpath "D:\MongoDB\data\db" --logpath "D:\MongoDB\data\log\MongoDB.log" --install --serviceName "MongoDB"
这里--MongoDB.log就是开始建立的日志文件,--serviceName "MongoDB" 服务名为MongoDB。
接着启动mongodb服务
> D:\MongoDB\bin>NET START MongoDB
打开任务管理器,可以看到进程已经启动
7.关闭服务和删除进程
> D:\MongoDB\bin>NET stop MongoDB (关闭服务)
>D:\MongoDB\bin>mongod --dbpath "D:\MongoDB\data\db" --logpath "D:\MongoDB\data\log\MongoDB.log" --remove --serviceName "MongoDB"
(删除,注意不是--install了)
二、使用mongodb
1.常用的命令
show dbs 显示数据库列表
use dbname 进入dbname数据库,大小写敏感,没有这个数据库也不要紧
show collections 显示数据库中的集合,相当于表格
2.创建&新增
db.users.save({"name":"lecaf"}) 创建了名为users的集合,并新增了一条{"name":"lecaf"}的数据
db.users.insert({"name":"ghost", "age":10}) 在users集合中插入一条新数据,,如果没有users这个集合,mongodb会自动创建
save()和insert()也存在着些许区别:若新增的数据主键已经存在,insert()会不做操作并提示错误,而save() 则更改原来的内容为新内容。
存在数据:{ _id : 1, " name " : " n1 "} ,_id是主键
insert({ _id : 1, " name " : " n2 " }) 会提示错误
save({ _id : 1, " name " : " n2 " }) 会把 n1 改为 n2 ,有update的作用。
3.删除
db.users.remove() 删除users集合下所有数据
db.users.remove({"name": "lecaf"}) 删除users集合下name=lecaf的数据
db.users.drop()或db.runCommand({"drop","users"}) 删除集合users
db.runCommand({"dropDatabase": 1}) 删除当前数据库
4.查找
db.users.find() 查找users集合中所有数据
db.users.findOne() 查找users集合中的第一条数据
5.修改
db.users.update({"name":"lecaf"}, {"age":10}) 修改name=lecaf的数据为age=10,第一个参数是查找条件,第二个参数是修改内容,除了主键,其他内容会被第二个参数的内容替换,主键不能修改,如图
三、高级应用
1.条件查找
db.collection.find({ "key" : value }) 查找key=value的数据
db.collection.find({ "key" : { $gt: value } }) key > value
db.collection.find({ "key" : { $lt: value } }) key
db.collection.find({ "key" : { $gte: value } }) key >= value
db.collection.find({ "key" : { $lte: value } }) key
db.collection.find({ "key" : { $gt: value1 , $lt: value2 } }) value1
db.collection.find({ "key" : { $ne: value } }) key value
db.collection.find({ "key" : { $mod : [ 10 , 1 ] } }) 取模运算,条件相当于key % 10 == 1 即key除以10余数为1的
db.collection.find({ "key" : { $nin: [ 1, 2, 3 ] } }) 不属于,条件相当于key的值不属于[ 1, 2, 3 ]中任何一个
db.collection.find({ "key" : { $in: [ 1, 2, 3 ] } }) 属于,条件相当于key等于[ 1, 2, 3 ]中任何一个
db.collection.find({ "key" : { $size: 1 } }) $size 数量、尺寸,条件相当于key的值的数量是1(key必须是数组,一个值的情况不能算是数量为1的数组)
db.collection.find({ "key" : { $exists : true|false } }) $exists 字段存在,true返回存在字段key的数据,false返回不存在字度key的数据
db.collection.find({ "key": /^val.*val$/i }) 正则,类似like;“i”忽略大小写,“m”支持多行
db.collection.find({ $or : [{a : 1}, {b : 2} ] }) $or或 (注意:MongoDB 1.5.3后版本可用),符合条件a=1的或者符合条件b=2的数据都会查询出来
db.collection.find({ "key": value , $or : [{ a : 1 } , { b : 2 }] }) 符合条件key=value ,同时符合其他两个条件中任意一个的数据
db.collection.find({ "key.subkey" :value }) 内嵌对象中的值匹配,注意:"key.subkey"必须加引号
db.collection.find({ "key": { $not : /^val.*val$/i } }) 这是一个与其他查询条件组合使用的操作符,不会单独使用。上述查询条件得到的结果集加上$not之后就能获得相反的集合。
2.排序
db.collection.find().sort({ "key1" : -1 ,"key2" : 1 }) 这里的1代表升序,-1代表降序
3.其他
db.collection.find().limit(5) 控制返回结果数量,如果参数是0,则当作没有约束,limit()将不起作用
db.collection.find().skip(5) 控制返回结果跳过多少数量,如果参数是0,则当作没有约束,skip()将不起作用,或者说跳过了0条
db.collection.find().skip(5).limit(5) 可用来做分页,跳过5条数据再取5条数据
db.collection.find().count(true) count()返回结果集的条数
db.collection.find().skip(5).limit(5).count(true) 在加入skip()和limit()这两个操作时,要获得实际返回的结果数,需要一个参数true,否则返回的是符合查询条件的结果总数

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



BitgetLaunchpool is a dynamic platform designed for all cryptocurrency enthusiasts. BitgetLaunchpool stands out with its unique offering. Here, you can stake your tokens to unlock more rewards, including airdrops, high returns, and a generous prize pool exclusive to early participants. What is BitgetLaunchpool? BitgetLaunchpool is a cryptocurrency platform where tokens can be staked and earned with user-friendly terms and conditions. By investing BGB or other tokens in Launchpool, users have the opportunity to receive free airdrops, earnings and participate in generous bonus pools. The income from pledged assets is calculated within T+1 hours, and the rewards are based on

According to news on July 28, Arm architecture CPU cores will be forced to adopt 64-bit starting from 2023, and 32-bit instructions will be gradually phased out. Google has restricted 32-bit applications on Android 15 in 2024, which means that new phones equipped with Android 15 starting in September 2024 will not be able to install and use 32-bit applications. In order to better improve the compatibility experience of Android applications and avoid the problem of applications being unable to be installed and used on new phones in the second half of 2024, Android manufacturers are requiring developers to perform 64-bit adaptation. According to reports, after August 31, 2024, the four Gold Label Alliance members OPPO, vivo, Xiaomi, and Lenovo will gradually clean up 32-bit applications in their stores, and implement relevant risk warnings and other measures for 32-bit applications that are not on the shelves. until

.NET 4.0 is used to create a variety of applications and it provides application developers with rich features including: object-oriented programming, flexibility, powerful architecture, cloud computing integration, performance optimization, extensive libraries, security, Scalability, data access, and mobile development support.

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

A few days ago, Google officially pushed the Android 15 Beta 4 update to eligible Pixel smartphone and tablet users. This marks that the Android 15 operating system has entered the platform stable stage, indicating that its stable version will be officially released with global users in the next few days. Meet. At the same time, this development also injects new vitality into Samsung Electronics' Galaxy device series to accelerate the development process of its OneUI7.0 version. 1.[Android15Beta4 promotes Samsung OneUI7.0 stable build](https://www.cnbeta.com/articles/tech/1427022.htm) With Android15Bet

By using the Go language's built-in testing framework, developers can easily write and run tests for their code. The test file ends with _test.go and contains the test function starting with Test, where the *testing.T parameter represents the test instance. Error information is recorded using t.Error(). Tests can be run by running the gotest command. Subtests allow test functions to be broken down into smaller parts and created via t.Run(). The practical case includes a test file written for the IsStringPalindrome() function in the utils package, which uses a series of input strings and expected output to test the correctness of the function.

PiNetwork is about to launch PiBank, a revolutionary mobile banking platform! PiNetwork today released a major update on Elmahrosa (Face) PIMISRBank, referred to as PiBank, which perfectly integrates traditional banking services with PiNetwork cryptocurrency functions to realize the atomic exchange of fiat currencies and cryptocurrencies (supports the swap between fiat currencies such as the US dollar, euro, and Indonesian rupiah with cryptocurrencies such as PiCoin, USDT, and USDC). What is the charm of PiBank? Let's find out! PiBank's main functions: One-stop management of bank accounts and cryptocurrency assets. Support real-time transactions and adopt biospecies
