Common MongoDB operation commands 2

Source: Internet
Author: User

 

  • Super User-related: Hide and expand the copy code

    1. Use Admin

    2. # Adding or modifying user passwords

    3. DB. adduser ('admin', 'pwd ')

    4. # Viewing the user list

    5. DB. system. Users. Find ()

    6. # User Authentication

    7. DB. Auth ('admin', 'pwd ')

    8. # Deleting a user

    9. DB. removeuser ('mongodb ')

    10. # View all users

    11. Show users

    12. # View all databases

    13. Show DBS

    14. # View all collections

    15. Show collections

    16. # View the status of each collection

    17. DB. printcollectionstats ()

    18. # View master-slave replication status

    19. DB. printreplicationinfo ()

    20. # Database Restoration

    21. DB. repairdatabase ()

    22. # Set the record profiling, 0 = off 1 = slow 2 = all

    23. DB. setprofilinglevel (1)

    24. # View profiling

    25. Show Profile

    26. # Copy a database

    27. DB. copydatabase ('mail _ ADDR ', 'mail _ addr_tmp ')

    28. # Delete collection

    29. DB. mail_addr.drop ()

    30. # Deleting the current database

    31. DB. dropdatabase ()

  • Client Connection collapse expand copy code

    1. /Usr/local/MongoDB/bin/Mongo user_addr-u user-P 'pwd'

  • Add, delete, modify, fold, and copy code

    1. # Store nested objects

    2. DB. foo. save ({'name': 'xy', 'address': {'city': 'beijinging', 'post': 100096}, 'phone': [138,139]})

    3. # Store array objects

    4. DB. user_addr.save ({'uid': 'yushunzhi @ sohu.com ', 'al': ['test-1@sohu.com', 'test-2@sohu.com ']})

    5. # Modify according to query conditions. If the query condition does not exist, insert multiple records.

    6. DB. Foo. Update ({'yy': 5}, {'$ set': {'xx': 2 }}, upsert = true, multi = true)

    7. # Delete records with YY = 5

    8. DB. Foo. Remove ({'yy': 5 })

    9. # Deleting all records

    10. DB. Foo. Remove ()

  • Index folding, expand, and copy code

    1. # Add an index: 1 (ascending),-1 (descending)

    2. DB. Things. ensureindex ({firstname: 1, lastname: 1 },{ unique: true });

    3. # Index sub-objects

    4. DB. user_addr.ensureindex ({'al. Em ': 1 })

    5. # Viewing index information

    6. DB. deliver_status.getindexes ()

    7. DB. deliver_status.getindexkeys ()

    8. # Deleting an index based on the index name

    9. DB. user_addr.dropindex ('al. em_1 ')

  • Query folding, expand, and copy code

    1. # Search for all

    2. DB. Foo. Find ()

    3. # Searching for a record

    4. DB. Foo. findone ()

    5. # Retrieve 10 records based on conditions

    6. DB. Foo. Find ({'msg ': 'Hello 1'}). Limit (10)

    7. # Sort sorting

    8. DB. deliver_status.find ({'from': 'yushunzhi @ sohu.com '}). Sort ({'dt',-1 })

    9. DB. deliver_status.find (). Sort ({'ct ':-1}). limit (1)

    10. # Count operation

    11. DB. user_addr.count ()

    12. # Distinct operations

    13. DB. Foo. Distinct ('msg ')

    14. #> Operation

    15. DB. Foo. Find ({"timestamp": {"$ GTE": 2 }})

    16. # Search for sub-objects

    17. DB. Foo. Find ({'address. City': 'beijing '})

  • Management

    Hide and expand copy code

    1. # View the collection data size

    2. DB. deliver_status.datasize ()

    3. # Viewing colleciont status

    4. DB. deliver_status.stats ()

    5. # Query the size of all indexes

    6. DB. deliver_status.totalindexsize ()

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.