peri hokiperihokiclarustologisticsrimashaop.comangelpaintingtools.co.invrjanaenergies.comgates of olympus perihoki jadi mesin uang paling gacortrik main baccarat perihoki ala pro bikin pemula jadi jutawanpantang pulang sebelum jackpot main dadu sicbo perihokipgsoft mahjong ways 2 muncul scatter merah tiap spin perihokicara menciptakan akun perihokigacor mahjong wins 3jackpot kembar mahjong wins 3 abc1131cara menang stabil rtp mahjong ways abc1131bu nurlina menang 197 juta mahjong ways 2mahasiswa cuan 421 juta karena pola mahjongpemilik warung menang 128 juta mahjong wins 3strategi bravy erika mahjong abc1131 terungkappemuda jambi taklukkan mahjong wins 3penjual jamu menang besar mahjong wins 5pemuda kampung raup 180 juta mahjong wayskisah inspiratif petani mahjong ways keberhasilanmahasiswa menang 92 juta mahjong waysmodal warung kopi bang ojak sukses mahjong waystanpa modal maxwin mahjong wins 3 strategibocoran mahjong ways spin kombinasicara bermain mahjong untuk meraih keberuntungankeajaiban mahjong kasir indomaret sukses beli tanahmeraup keuntungan maksimal di mahjong winsrahasia kemenangan mahjong ways spin slow

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Chèn Document trong MongoDB - Freetuts
MONGODB CĂN BẢN
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Chèn Document trong MongoDB

Một trong những nhiệm vụ quan trọng khi làm việc với MongoDB là thêm dữ liệu mới vào trong cơ sở dữ liệu của bạn. Trong bài viết này, mình sẽ tìm hiểu cách sử dụng phương thức insert() để chèn các tài liệu (document) vào collection trong MongoDB.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Cách thêm dữ liệu vào MongoDB

Để thêm dữ liệu vào collection trong MongoDB, bạn có thể sử dụng phương thức insert() hoặc save(). Ở đây, chúng ta sẽ tập trung vào phương thức insert().

Cú pháp cơ bản của lệnh insert() như sau:

db.COLLECTION_NAME.insert(document)

Ví dụ:

db.mycol.insert({
   _id: ObjectId(7df78ad8902c),
   title: 'MongoDB Overview', 
   description: 'MongoDB is no SQL database',
   by: 'tutorials point',
   url: 'https://freetuts.net/',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 100
})

Ở đây, mycol là tên của collection. Nếu collection này chưa tồn tại trong cơ sở dữ liệu, MongoDB sẽ tự động tạo collection này và sau đó chèn tài liệu vào nó.

Bài viết này được đăng tại [free tuts .net]

Ví dụ chèn nhiều tài liệu trong MongoDB

Để chèn nhiều tài liệu trong một lệnh duy nhất, bạn có thể truyền một mảng các tài liệu vào phương thức insert().

Ví dụ:

db.post.insert([
{
   title: 'MongoDB Overview', 
   description: 'MongoDB is a NoSQL database',
   by: 'tutorials point',
   url: 'https://freetuts.net/',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 100
},
{
   title: 'NoSQL Database', 
   description: 'NoSQL databases don't have tables',
   by: 'tutorials point',
   url: 'https://freetuts.net/',
   tags: ['mongodb', 'database', 'NoSQL'],
   likes: 20, 
   comments: [  
      {
         user:'user1',
         message: 'My first comment',
         dateCreated: new Date(2013,11,10,2,35),
         like: 0 
      }
   ]
}
])

Sử dụng phương thức save()

Ngoài phương thức insert(), bạn có thể sử dụng phương thức save() để thêm dữ liệu vào MongoDB. Nếu bạn không xác định _id trong tài liệu, thì phương thức save() sẽ thực hiện việc thêm dữ liệu giống như insert(). Tuy nhiên, nếu bạn xác định _id, nó sẽ thay thế toàn bộ dữ liệu của tài liệu chứa _id khi được xác định trong phương thức save().

Kết bài

Trong bài viết này, mình đã khám phá cách thêm dữ liệu vào MongoDB bằng cách sử dụng phương thức insert(). Mình đã thấy cú pháp cơ bản và cách sử dụng nó để chèn tài liệu (document) vào các collection trong cơ sở dữ liệu MongoDB. Mình cũng đã tìm hiểu về việc chèn nhiều tài liệu trong một lệnh và sử dụng phương thức save() để quản lý dữ liệu.

Thêm dữ liệu là một phần quan trọng trong quá trình làm việc với cơ sở dữ liệu MongoDB. Với hiểu biết về cách thực hiện thao tác này, bạn có thể bắt đầu xây dựng và quản lý cơ sở dữ liệu của mình một cách hiệu quả trong ứng dụng MongoDB của bạn. Hãy tiếp tục tìm hiểu các chức năng khác của MongoDB để tận dụng toàn bộ tiềm năng của nó trong phát triển ứng dụng và lưu trữ dữ liệu.

Cùng chuyên mục:

Sử dụng Text Search trong MongoDB

Sử dụng Text Search trong MongoDB

Map Reduce trong MongoDB

Map Reduce trong MongoDB

ObjectId trong MongoDB

ObjectId trong MongoDB

Hoạt động nguyên tử (Atomic Operation) trong MongoDB

Hoạt động nguyên tử (Atomic Operation) trong MongoDB

Phân tích truy vấn trong MongoDB

Phân tích truy vấn trong MongoDB

Covered Query trong MongoDB

Covered Query trong MongoDB

Tham chiếu Database trong MongoDB

Tham chiếu Database trong MongoDB

Relationship trong MongoDB

Relationship trong MongoDB

Tạo backup và restore trong mongodb

Tạo backup và restore trong mongodb

Shard trong MongoDB

Shard trong MongoDB

Replica Set trong MongoDB

Replica Set trong MongoDB

Aggregation trong MongoDB

Aggregation trong MongoDB

Chỉ mục (Index) trong MongoDB

Chỉ mục (Index) trong MongoDB

Sắp xếp bản ghi trong MongoDB

Sắp xếp bản ghi trong MongoDB

Giới hạn bản ghi trong MongoDB

Giới hạn bản ghi trong MongoDB

Projection trong MongoDB

Projection trong MongoDB

Xóa Document trong MongoDB

Xóa Document trong MongoDB

Cập nhật Document trong MongoDB

Cập nhật Document trong MongoDB

Truy vấn Document trong MongoDB

Truy vấn Document trong MongoDB

Kiểu dữ liệu trong MongoDB

Kiểu dữ liệu trong MongoDB

Top