Giter Club home page Giter Club logo

mongodb's Introduction

Kiểu dữ liệu trong MongoDB

  • Chuỗi: Chuỗi trong MongoDB phải là UTF-8 hợp lệ.
  • Số nguyên: Số nguyên có thể là 32 bit hoặc 64 bit phụ thuộc vào Server.
  • Boolean: lưu trữ giá bị boolean true/false.
  • Double: Lưu các giá trị số thực dấu chấm động.
  • Min/Max keys: Sử dụng để so sánh một giá trị với các phần tử BSON thấp nhất và cao nhất.
  • Mảng: Lưu trữ các mảng, hoặc danh sách, hoặc nhiều giá trị vào trong một key.
  • Timestamp: Ghi chép hoặc đánh dấu thời điểm một Document được sửa đổi hoặc được thêm vào.
  • Object: Sử dụng cho các Document được nhúng vào.
  • Null: Lưu một giá trị null.
  • Symbol: Sử dụng giống như một chuỗi, tuy nhiên , nói chung nó được dành riêng cho các ngôn ngữ mà sử dụng kiểu symbol cụ thể.
  • Date: Sử dụng để lưu trữ date và time hiện tại trong định dạng UNIX time. Có thể xác định date time riêng bằng việc tạo đối tượng Date và truyền ngày, tháng, năm vào trong đó.
  • Object ID: Để lưu trữ ID của Document.
  • Binary data: Sử dụng để lưu trữ dữ liệu nhị phân.
  • Code: Lưu trữ JavaScript code vào trong Document.
  • Regular expression: Để lưu trữ Regular Expression.

Document

INSERT

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

$ db.COLLECTION_NAME.insert(document)

Note:

  • document có thể là một object JSON hoặc một mảng JSON.
  • _id: ObjectId(7df78ad8902c): _id là một số thập lục phân duy nhất, dài 12byte.

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

$ db.COLLECTION_NAME.insert(document)

Note:

  • Nếu không xác định _id: thì phương thức save() sẽ làm việc giống như phương thức insert().
  • Nếu bạn xác định _id: thì sẽ là UPDATE toàn bộ dữ liệu của document chứa _id đó.

SELECT

  • Phương thức find() : Hiển thị tất cả Document ở dạng không có cấu trúc.

$ db.COLLECTION_NAME.find()

  • Projection: chỉ chọn dữ liệu cần lấy thay vì toàn bộ dữ liệu của document.

$ db.collection.find({}, {KEYA:1, KEYB:0})

  • Key là 1 thì sẽ được hiện ra, 0 để ẩn.

  • Phương thức pretty() : Để hiển thị các kết quả theo một cách đã được định dạng

$ db.mycol.find().pretty()

Ngoài phương thức find(), phương thức findOne() sẽ chỉ trả về một Document.

Truy vấn theo điều kiện

Phép toánCú phápVí dụMệnh đề WHERE tương đương
Equality{<key>:<value>}db.mycol.find({"by":"tutorials point"}).pretty()where by = 'tutorials point'
Less Than{<key>:{$lt:<value>}}db.mycol.find({"likes":{$lt:50}}).pretty()where likes < 50
Less Than Equals{<key>:{$lte:<value>}}db.mycol.find({"likes":{$lte:50}}).pretty()where likes <= 50
Greater Than{<key>:{$gt:<value>}}db.mycol.find({"likes":{$gt:50}}).pretty()where likes > 50
Greater Than Equals{<key>:{$gte:<value>}}db.mycol.find({"likes":{$gte:50}}).pretty()where likes >= 50
Not Equals{<key>:{$ne:<value>}}db.mycol.find({"likes":{$ne:50}}).pretty()where likes != 50

AND

Trong phương thức find() nếu truyền nhiều key bằng cách phân biệt chúng bởi dấu phẩy, thì MongoDB xem nó như là điều kiện AND:

$ db.collectionname.find({key1:value1, key2:value2}).pretty()

OR

Cần sử dụng từ khoá $or

$ db.collectionname.find({ $or:[{"by":"tutorial spoint"}, {"title":"MongoDB Overview"}] }).pretty()

Ví dụ trên sẽ hiển thị tất cả bản ghi trong collection: collectionname được viết bởi 'tutorial spoint' hoặc có title là 'MongoDB Overview'

AND và OR

$ db.collectionname.find({ "likes":{$gt:10}, $or:[{"by":"spoint", {"title":"MongoDB"} }] }).pretty()

tương đương với truy vấn SQL là: 'where likes>10 AND (by = 'point' OR title = 'MongoDB')'

UPDATE

MongoDB cung cấp các phương thức để update các bản ghi trong 1 collection:

  • db.collection.updateOne()
  • db.collection.updateMany()
  • db.collection.replaceOne()
  • db.collection.update()

Các phương thức sau cũng có thể update được:

  • db.collection.findOneAndReplace()
  • db.collection.findOneAndUpdate()
  • db.collection.findAndModify()
  • db.collection.save()
  • db.collection.bulkWrite()

DELETE

  • db.collection.remove()

  • db.collection.deleteOne()

  • db.collection.deleteMany()

  • db.collection.findOneAndDelete()

  • db.collection.findOneAndModify()

  • db.collection.bulkWrite()

Phương thức remove() có thể truyền như sau

$ db.collection.remove(DELLETION_CRITTERIA, JUST_ONE)

  • DELLETION_CRITTERIA: (tuỳ chọn) xác định Document để xoá
  • JUST_ONE: (tuỳ chọn) nếu là true hoặc 1, thì chỉ xoá 1 Document.
  • nếu không truyền gì vào thì sẽ xoá tất cả các documents trong collection đó.

LIMIT

Giới hạn bản ghi lấy ra

$ db.collection.find().limit(NUMBER)

SKIP

Nhảy qua 10 bản ghi và lấy ra 20 bản ghi. Giá trị mặc định trong phương thức skip() là 0

$ db.collection.find({}, {"title":1, _id:0}).limit(20).skip(10)

SORT

1: tăng dần, -1: giảm dần. Câu lệnh sau sắp xếp các bản ghi theo title tăng dần. Nếu không xác định thứ tự sắp xếp thì phương thức sort() sẽ hiển thị document theo thứ tự tăng dần.

$ db.collection.find().sort({"title":1})

mongodb's People

Contributors

mozaa-vn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.