当前位置:网站首页>Simple use of mongodb database
Simple use of mongodb database
2022-07-25 00:23:00 【Big player】
MongoDB Use of database
Adding data to the database :
Database insertion
db. Collection name .insert({})–> Insert a
db. Collection name .insert([{},{},…])–> Insert multiple
db. Collection name .insertOne({})–> Insert a
db. Collection name .insertMany([{},{},…])–> Insert multiple
// notes : Every document in the database has a unique id Property value , You can specify it yourself, but make sure it's unique
Search operation of database :
db. Collection name .find()–> Find all documents in the database collection
db. Collection name .findOne({})–> Find the first document that meets the criteria
db. Collection name .find()
Database update operation :
db. Collection name .update({},{})// Accept two objects , The first object represents the object being searched , The second object represents the data to be updated , however update By default, the second object will replace the old object , If you want to modify a property , You need to use a conditional operator
update() By default, only the first qualified document will be modified , If you want to modify more than one parameter, add a third parameter multi:true
db. Collection name .updateMany()– Modify multiple documents
db. Collection name .updateOne()– Modify a document
Database delete operation :
db. Collection name .deleteOne({})– Delete the first document that meets the criteria
db. Collection name .deleteMany({})– Delete multiple documents that meet the conditions
db. Collection name .remove({})– Delete eligible documents , By default, multiple
Delete database :db.dropDatebase()
Delete the collection :db. Collection name .drop()
Conditional operators :
$gt Greater than $lt Less than $gte Greater than or equal to $lte Less than or equal to eq be equal to $or:[{},{},…] Choose a relationship
limit()- Set the upper limit of data display , Used for paging
skip()- Used to skip the specified amount of data
stay Python Chinese vs mongodb Conduct operation code
Import related packages
import pymongo
from pymongo import MongoClient
Create databases and tables
# MongDB Connect to database
conn = MongoClient('localhost', 27017)
db = conn['history']
col = db['log']
insert data ( When the data is a dictionary, the following code may be required for format conversion )
from bson import json_util
data1=json.loads(json_util.dumps(data))
col.insert_one(data1)
Find data
record = col.find(sort=[("_id", pymongo.DESCENDING)])
records = []
for r in record:
records.append(r)
print(r)
Output results :
record It is a collection of many dictionaries
Pass the dictionary through the list
Use table style to display in the background
<section class="counts section-bg">
<div class="container">
<table class="table table-striped table-hover table-condensed table-bordered ">
<tr class="info">
<th> Content </th>
<th> title </th>
</tr>
{% for record in records %}
<tr class="success">
<td>{
{record['name']}}</td>
<td>{
{record['content']}}</td>
</tr>
{% endfor %}
</table>
</div>
</section><!-- End Counts Section -->
边栏推荐
- Find the median of two numbers in the fourth question of C language deduction (three methods)
- Usage of atomicinteger (counter)
- Weekly summary (*66): next five years
- NXP i.mx6q development board software and hardware are all open source, and the schematic diagram of the core board is provided
- Regular expression learning
- 2022 Henan Mengxin League game (2): Henan University of technology d-pair
- 云图
- Research and Multisim Simulation of linear circuit characteristics (engineering documents attached)
- Install K6 test tool
- [acwing周赛复盘] 第 61 场周赛20220723
猜你喜欢

Oracle is not null cannot filter null values

The font changes with the change of the form
![[LeetCode周赛复盘] 第 303 场周赛20220724](/img/ba/0f16f1f42e4a2593ec0124f23b30d7.png)
[LeetCode周赛复盘] 第 303 场周赛20220724

数组中只出现一次的两个数字

Implement a avatar looping control

C language: deep analysis function stack frame
![[leetcode weekly replay] game 83 biweekly 20220723](/img/db/c264c94ca3307d4363d3cf7f5d770b.png)
[leetcode weekly replay] game 83 biweekly 20220723

2. Load test

BGP机房和BGP

50 places are limited to open | with the news of oceanbase's annual press conference coming!
随机推荐
The model needs to use two losses_ FN, how to operate?
What is the function of transdata operator and whether it can optimize performance
Live broadcast preview | online seminar on open source security governance models and tools
[help] mindspire training based on ascend910 cannot reproduce the model effect on GPU
Internal network mapping port to external network
技术操作
Detailed usage of iperf
Quartus:17.1版本的Quartus安装Cyclone 10 LP器件库
Simple operation K6
Install K6 test tool
MySQL common basic commands
[mindspore ascend] [user defined operator] graph_ In mode, customize how to traverse tensor
数组中只出现一次的两个数字
[Bert] transformer/bert/attention interview questions and answers
Soft test --- fundamentals of programming language (Part 2)
Optaplanner will abandon DRL (drools) scoring method!!!
Usage of atomicinteger (counter)
Qt学习-利用数据库单例完成 登录匹配 + 注册 功能实现
LeetCode_ 6124_ The first letter that appears twice
[Bert] QA, reading comprehension, information retrieval