当前位置:网站首页>Node の MongoDB Driver
Node の MongoDB Driver
2022-07-05 09:29:00 【InfoQ】
order
MongoDBCompassMongoDB- 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 mongodbmongodb
Connect
mongodbMongoClient
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();
});
}

mongodbTestcompass




MongoClient.dbconst 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
collectionscollectionName// 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
MongoDBfind
// Read the data in the collection
const res = await c3.find().toArray();
console.log(res)
findprojectionMongoServerError: Cannot do inclusion on field name in exclusion projection_idage// 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)

边栏推荐
猜你喜欢

Kotlin introductory notes (V) classes and objects, inheritance, constructors

SMT32H7系列DMA和DMAMUX的一点理解

Figure neural network + comparative learning, where to go next?

Hosting environment API

LeetCode 556. 下一个更大元素 III

Composition of applet code

Shutter uses overlay to realize global pop-up

Hi Fun Summer, play SQL planner with starrocks!

Project practice | excel export function

mysql安装配置以及创建数据库和表
随机推荐
LeetCode 31. 下一个排列
nodejs_ 01_ fs. readFile
[beauty of algebra] solution method of linear equations ax=0
Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
Svg optimization by svgo
信息與熵,你想知道的都在這裏了
LeetCode 503. 下一个更大元素 II
Cloud computing technology hotspot
一次 Keepalived 高可用的事故,让我重学了一遍它
Can't find the activitymainbinding class? The pit I stepped on when I just learned databinding
【对象数组a与对象数组b取出id不同元素赋值给新的数组】
微信小程序获取住户地区信息
Kotlin introductory notes (V) classes and objects, inheritance, constructors
STM32简易多级菜单(数组查表法)
[team PK competition] the task of this week has been opened | question answering challenge to consolidate the knowledge of commodity details
浅谈Label Smoothing技术
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
OpenGL - Model Loading
High performance spark_ Transformation performance
NIPS2021 | 超越GraphCL,GNN+对比学习的节点分类新SOTA