Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Database \ MongoDB relations, references, index query coverage     - Depth Java Singleton (Programming)

- Installation Mesos + Marathon + Zookeeper under CentOS 7 (Server)

- Get the Linux device PCI ID method (Linux)

- Linux system security configuration (Linux)

- Use Epoll develop high-performance application server on Linux (Server)

- How to create a remote (Linux)

- Linux command to view the system status (Linux)

- Use XtraBackup be physical standby database MySQL (Database)

- Reported too many open files Linux solutions (Server)

- Steps to build MPICH2 development environment on CentOS 6.4 (Linux)

- Nginx + uWSGI + Django + Python Application Architecture Deployment (Server)

- Wi-Fi hackers use to attack your seven methods (Linux)

- CentOS 7 - use cgroups limit process resource (Linux)

- Use Android Studio and Gradle packaged multi-version APK (Programming)

- OpenDaylight Helium version installed (Linux)

- JavaScript object - Flexible and dangerous (Programming)

- Ubuntu 14.04 forget solution root password (Linux)

- Compile and install Redis and register as a system service under RedHat5.8 environment (Database)

- Linux / Windows setup is complete port range (Linux)

- Installation of Python2.7.10 under CentOS 6.4 (Linux)

 
         
  MongoDB relations, references, index query coverage
     
  Add Date : 2018-11-21      
         
         
         
 

First, the relationship

Relations

MongoDB representation interlinkages between multiple documents in logic. You can establish contact by embedding and references between documents. Relationship in MongoDB can be: 1 to 1,1 for more and more of the 1-many.

A user can use more than one address, which is typical of many relationship.

user documentation can be:

{
  "_id": ObjectId ( "52ffc33cd85242f436000001"),
  "name": "Tom Hanks",
  "contact": "987654321",
  "dob": "01-01-1991"
}

address document can be:

{
  "_id": ObjectId ( "52ffc4a5d85242602e000000"),
  "building": "22 A, Indiana Apt",
  "pincode": 123456 ,
  "city": "Los Angeles",
  "state": "California"
}

1, the embedded relationship

Using the embedded method, the address of the user's documents into the document

{
    "_id": ObjectId ( "52ffc33cd85242f436000001"),
  "contact": "987654321",
  "dob": "01- 01-1991 ",
 " name ":" Tom Benzamin ",
 " address ": [
      {
        "building": "22 A, Indiana Apt",
        "pincode": 123456,
        "city": "Los Angeles",
        "state": "California"
     },
      {
        "building": "170 A, Acropolis Apt",
        "pincode": 456789,
        "city": "Chicago",
        "state": "Illinois"
     }]
}

If so, then you can save so get the user's address:

db.users.findOne ({ "name": "Tom Benzamin"}, { "address": 1})

The disadvantage of this data structure is that if users and address the increasing amount of data becomes larger and larger, will affect the read and write performance.

2, a reference approach

This method is similar to a relational database foreign key, will address the _id saved to user documentation

{
  "_id": ObjectId ( "52ffc33cd85242f436000001"),
  "contact": "987654321",
  "dob": "01-01- 1991 ",
 " name ":" Tom Benzamin ",
 " address_ids ": [
      ObjectId (" 52ffc4a5d85242602e000000 "),
      ObjectId ( "52ffc4a5d85242602e000001")
 ]
}

We can read the user's address object id (ObjectId) to get detailed information about the user's address. This method requires two queries, the first query object id user's address (ObjectId), the second address for more information on the user's query by id.

var result = db.users.findOne ({ "name": "Tom Benzamin"}, { "address_ids": 1})
var addresses = db.address.find ({ "_ id": { "$ in": result [ "address_ids"]}})

Second, the database references

mongodb There are two references: a reference manual (Manual References) and DBRefs

If we have a different set (address_home, address_office, address_mailing, etc.) stored in a different address (address, office address, mailing address, etc.). This time when we call different address, you need to specify a collection, a document reference document from multiple collections, we should use DBRefs.

DBRef form of:

{$ ref:, $ id:, $ db:  }

where $ ref: collection name, $ id: reference id, $ db: Database name (optional).

The following example uses a user data document DBRef, field address:

{
  "_id": ObjectId ( "53402597d852426020000002"),
  "address": {
  "$ ref": "address_home",
  "$ id": ObjectId ( "534009e4d852427820000002"),
  "$ db": "w3cschoolcc"},
  "contact": "987654321",
  "dob": "01-01-1991",
  "name": "Tom Benzamin"
}

address DBRef field specifies the address referenced documents are under address_home collection w3cschoolcc database, id is 534009e4d852427820000002.

The following code, we specify $ ref parameter (address_home set) to find the collection at the specified id user address information:

var user = db.users.findOne ({ "name": "Tom Benzamin"})
var dbRef = user.address
db [dbRef $ ref.] .findOne ({ "_ Id ": (. dbRef $ id)})

The above examples returns address_home collection address data:

{
  "_id": ObjectId ( "534009e4d852427820000002"),
  "building": "22 A, Indiana Apt",
  "pincode": 123456 ,
  "city": "Los Angeles",
  "state": "California"
}

Third, coverage index query

cover the following query is a query:
• all query fields are part of the index
• The query returns all the fields in the same index

Because all appear in the query field is part of the index, MongoDB data files without having to retrieve the entire match query terms and return query results using the same index. Because the index reside in RAM, the access to data than to read data much faster by scanning documents from the index.

Example: user set:

 

{
  "_id": ObjectId ( "53402597d852426020000002"),
  "contact": "987654321",
  "dob": "01-01- 1991 ",
 " gender ":" M ",
 " name ":" Tom Benzamin ",
 " user_name ":" tombenzamin "
}

Create joint index, gender and field user_name

db.users.ensureIndex ({gender: 1, user_name: 1})

Now, the index will cover the following query:

db.users.find ({gender: "M"}, {user_name: 1, _id: 0})

For the above query, MongoDB's not going to file for a database. Instead, it extracts the data from the index, which is very fast data query. Since our index does not include the _id field, _id will be returned by default in the query, we can concentrate exclude it in MongoDB query results. The following examples are not ruled _id, the query will not be overwritten:

db.users.find ({gender: "M"}, {user_name: 1})

If all of the index field is an array index query coverage can not be used, all index field is a sub-document.

     
         
         
         
  More:      
 
- Ubuntu 14.04 VirtualBox can not start solution (Linux)
- Linux System Administrator common interview questions and answers 30 (Linux)
- Justniffer installed on Ubuntu 15.04 (Linux)
- Docker ecosystem security is gradually maturing (Server)
- JDK tools jstat (Linux)
- PULL operation mechanism parsing XML Comments (Programming)
- Linux user directory (Linux)
- Build ftp server under CentOS 6.5 (Server)
- MySQL master-slave database configuration and error handling Raiders (Database)
- ImageMagick Tutorial: How to cut images in Linux command line (Linux)
- Intel Graphics Installer 1.0.3 released, support for Ubuntu (Linux)
- Upgrading Oracle 11.2.0.1.0 to 11.2.0.3.0 (Database)
- Openfire Hazelcast cluster Detailed (Server)
- Memcache explain in detail (Server)
- MySQL and MariaDB traditional master-slave cluster configuration (Database)
- To protect the temporary files for Linux security (Linux)
- LogStash log analysis display system (Linux)
- openSUSE 13.1 / 13.2 installation SoundConverter 2.1.2 (Linux)
- How to install and use the malware detection tool LMD and ClamAV antivirus engine on Linux (Linux)
- Linux system started to learn: the Linux syslog (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.