Main Content

createCollection

CreateMongoDBcollection

ThecreateCollectionfunction will be removed in a future release. Use thecreateCollectionfunction of the MongoDB®C++ interface instead.

Description

example

createCollection(conn,collection)creates a collection in MongoDB by using the MongoDB connection.

Examples

collapse all

Connect to MongoDB and create a collection.

Create a MongoDB connection to the databasemongotest. Here, the database serverdbtb01hosts this database using port number27017.

server ="dbtb01"; port = 27017; dbname ="mongotest"; conn = mongo(server,port,dbname)
conn = mongo with properties: Database: 'mongotest' UserName: '' Server: {'dbtb01'} Port: 27017 CollectionNames: {'airlinesmall', 'employee', 'largedata' ... and 3 more} TotalDocuments: 23485919

connis themongoobject that contains the MongoDB connection. The object properties contain information about the connection and the database.

  • The database name ismongotest.

  • The user name is blank.

  • The database server isdbtb01.

  • The port number is27017.

  • This database contains six document collections. The first three collection names areairlinesmall,employee, andlargedata.

  • This database contains 23,485,919 documents.

Verify the MongoDB connection.

isopen(conn)
ans = logical 1

The database connection is successful because theisopenfunction returns1. Otherwise, the database connection is closed.

Create a collection in the database using the MongoDB connection. Specify the collection nametaxidata.

collection ="taxidata"; createCollection(conn,collection)

Display the collections in the database by using theCollectionNamesproperty. The database contains the new collectiontaxidata.

conn.CollectionNames
ans = 1×7 cell array Columns 1 through 5 {'airlinesmall'} {'employee'} {'largedata'} {'nyctaxi'} {'product'} Columns 6 through 7 {'restaurants'} {'taxidata'}

Close the MongoDB connection.

close(conn)

Input Arguments

collapse all

MongoDB connection, specified as amongoobject.

Collection name, specified as a string scalar.

Example:"taxidata"

Data Types:string

版本历史

Introduced in R2017b