当前位置:网站首页>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)
边栏推荐
- 微信小程序获取住户地区信息
- scipy. misc. imread()
- [beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
- 【el-table如何禁用】
- Node の MongoDB Driver
- Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
- nodejs_ 01_ fs. readFile
- Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
- Nodejs modularization
- Greendao reported an error in qigsaw, could not init daoconfig
猜你喜欢
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
信息與熵,你想知道的都在這裏了
【数组的中的某个属性的监听】
LeetCode 31. Next spread
Rebuild my 3D world [open source] [serialization-1]
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
NIPS2021 | 超越GraphCL,GNN+对比学习的节点分类新SOTA
C语言-从键盘输入数组二维数组a,将a中3×5矩阵中第3列的元素左移到第0列,第3列以后的每列元素行依次左移,原来左边的各列依次绕到右边
Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]
Principle and performance analysis of lepton lossless compression
随机推荐
Wxml template syntax
Using request headers to develop multi terminal applications
Can't find the activitymainbinding class? The pit I stepped on when I just learned databinding
Project practice | excel export function
Nips2021 | new SOTA for node classification beyond graphcl, gnn+ comparative learning
MYSQL 对字符串类型排序不生效问题
2310. The number of bits is the sum of integers of K
测试老鸟浅谈unittest和pytest的区别
C form click event did not respond
Applet customization component
项目实战 | Excel导出功能
C # image difference comparison: image subtraction (pointer method, high speed)
Progressive JPEG pictures and related
Kotlin introductory notes (I) kotlin variables and non variables
Kotlin introductory notes (V) classes and objects, inheritance, constructors
LeetCode 503. 下一个更大元素 II
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
C语言-从键盘输入数组二维数组a,将a中3×5矩阵中第3列的元素左移到第0列,第3列以后的每列元素行依次左移,原来左边的各列依次绕到右边
OpenGL - Lighting
NIPS2021 | 超越GraphCL,GNN+对比学习的节点分类新SOTA