当前位置:网站首页>Node の MongoDB Driver
Node の MongoDB Driver
2022-07-05 09:29:00 【InfoQ】
order
MongoDB
Compass
MongoDB
- mongodb: MongoDB There is a basic library
mongodb
(Node.js MongoDB Driver)
- Mongoose: Now the mainstream Library , Better in the amount of code , Just add, delete, modify and check the recommendation
mongodb
download
yarn add mongodb
mongodb
Connect
mongodb
MongoClient
const MongoClient = require('mongodb').MongoClient;
const baseUrl = 'mongodb://127.0.0.1:27017/mongodbTest'
function TestConnect() {
MongoClient.connect(baseUrl, function(err, db) {
if (err) throw err;
console.log(" Database created !");
db.close();
});
}
mongodbTest
compass
MongoClient.db
const baseUrl = 'mongodb://127.0.0.1:27017'
// Create objects
const client = new MongoClient(baseUrl);
async function TestConnect() {
try {
// Connect to database
await client.connect();
// Use the previously manually created mongodbTest database
const db = await client.db('mongodbTest');
console.log(db.databaseName)
// Create a new collection ( If the database is created by the code , Then you must create a set )
db.createCollection('collection01')
} catch(e) {
console.log(e)
await client.close();
}
}
module.exports = { TestConnect }
Get collection list
collections
collectionName
// Connect to database
await client.connect();
// Use the previously manually created mongodbTest database
const db = await client.db('mongodbTest');
// Get all collections under the current database
let collections = await db.collections();
// Print set name
collections.forEach(item=>{
console.log(item.collectionName)
})
Create a collection and insert data
// Create a new collection
db.createCollection('collection03', { autoIndexId: true }, (err, res)=>{
if (err) console.log(' Collection creation failed ');
console.log(res.collectionName) // collection03
})
insertOne
// Use set collection03
let c3 = db.collection('collection03');
// Define documents
let doc = {
name: ' Zhang San ',
age: 18,
hobby: ' Hit Li Si ',
}
// Insert the document into the collection
const result = await c3.insertOne(doc);
- insert: Insert a single document or an array of documents MongoDB. If the incoming document does not contain \u id Field , The driver will add a , Thus changing the document
- insertMany: Insert the document array MongoDB. If the incoming document does not contain \u id Field , The driver will add a , Thus changing the document
- insertOne: Insert a single document MongoDB. If the incoming document does not contain \u id Field , The driver will add a , Thus changing the document
let doc = [
{
name: ' Li Si ',
age: 18,
hobby: ' Beat Wang Wu ',
},
{
name: ' Wang Wu ',
age: 18,
hobby: ' Open three ',
},
]
// Insert the document into the collection
const result = await c3.insertMany(doc);
Read the data in the collection
MongoDB
find
// Read the data in the collection
const res = await c3.find().toArray();
console.log(res)
find
projection
MongoServerError: Cannot do inclusion on field name in exclusion projection
_id
age
// Read the data in the collection
const res = await c3.find({}, {
projection: { _id: 0, age: 0 }
}).toArray();
console.log(res)
find
{}
// Read the data in the collection
const res = await c3.find({
name: / Zhang /
}, {
projection: { _id: 0, age: 0 }
}).toArray();
console.log(res)
Delete set information
// Read the data in the collection
const res = await c3.deleteOne({
name: / Zhang /
});
console.log(res) // { acknowledged: true, deletedCount: 1 }
Update collection
// Read the data in the collection
let newvalue = { $set: { hobby: ' Hit Li Si ' } }
const res = await c3.updateOne({
name: / king /
}, newvalue);
console.log(res)
边栏推荐
- fs. Path module
- Android privacy sandbox developer preview 3: privacy, security and personalized experience
- 微信小程序获取住户地区信息
- Kotlin introductory notes (VI) interface and function visibility modifiers
- Node の MongoDB Driver
- Applet (global data sharing)
- 使用el-upload封装得组件怎么清空已上传附件
- nodejs_ 01_ fs. readFile
- MySQL does not take effect in sorting string types
- Analysis of eventbus source code
猜你喜欢
OpenGL - Coordinate Systems
Rebuild my 3D world [open source] [serialization-2]
初识结构体
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
Priority queue (heap)
High performance spark_ Transformation performance
【sourceTree配置SSH及使用】
fs. Path module
Android 隐私沙盒开发者预览版 3: 隐私安全和个性化体验全都要
Editor use of VI and VIM
随机推荐
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
【对象数组a与对象数组b取出id不同元素赋值给新的数组】
[beauty of algebra] solution method of linear equations ax=0
Wxss template syntax
Talking about the difference between unittest and pytest
OpenGL - Lighting
2311. 小于等于 K 的最长二进制子序列
【饿了么动态表格】
Applet customization component
2309. 兼具大小写的最好英文字母
Svg optimization by svgo
【ManageEngine】如何利用好OpManager的报表功能
高性能Spark_transformation性能
It's too difficult to use. Long articles plus pictures and texts will only be written in short articles in the future
阿里十年测试带你走进APP测试的世界
Hosting environment API
浅谈Label Smoothing技术
微信小程序获取住户地区信息
Priority queue (heap)
STM32 simple multi-level menu (array table lookup method)