remove
Remove one or multiple documents from MongoDB collection
The remove
function will be removed in a future release. Use the
remove
function of the MongoDB® C++ interface instead.
Description
returns the number of documents removed from a collection using the MongoDB connection. Use a MongoDB query to specify removing one or multiple documents.n
= remove(conn
,collection
,mongoquery
)
Examples
Remove Documents Using MongoDB Query
Connect to MongoDB and remove documents from a collection. Specify a MongoDB query to determine which documents to remove. Here, the collection represents employee data.
Create a MongoDB connection to the database mongotest
. Here, the database server dbtb01
hosts this database using port number 27017
.
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
conn
is the
mongo
object that contains the MongoDB connection. The object properties contain information about the connection and the
database.
The database name is
mongotest
.The user name is blank.
The database server is
dbtb01
.The port number is
27017
.This database contains six document collections. The first three collection names are
airlinesmall
,employee
, andlargedata
.This database contains 23,485,919 documents.
Verify the MongoDB connection.
isopen(conn)
ans = logical 1
The database connection is successful because the isopen
function returns 1
. Otherwise, the database connection is closed.
Create a MongoDB query to identify documents to remove. Here, specify the
employee
collection. Create the MongoDB query to identify documents in the Sales department.
collection = "employee"; mongoquery = '{"department":"Sales"}';
Remove documents using the MongoDB query. The remove
function removes six
documents from the collection.
n = remove(conn,collection,mongoquery)
n = 6
Close the MongoDB connection.
close(conn)
Remove All Documents from Collection
Connect to MongoDB and remove all documents from a collection.
Create a MongoDB connection to the database mongotest
. Here, the database server dbtb01
hosts this database using port number 27017
.
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
conn
is the
mongo
object that contains the MongoDB connection. The object properties contain information about the connection and the
database.
The database name is
mongotest
.The user name is blank.
The database server is
dbtb01
.The port number is
27017
.This database contains six document collections. The first three collection names are
airlinesmall
,employee
, andlargedata
.This database contains 23,485,919 documents.
Verify the MongoDB connection.
isopen(conn)
ans = logical 1
The database connection is successful because the isopen
function returns 1
. Otherwise, the database connection is closed.
Remove all documents from the employee
collection. Use
an empty MongoDB query to specify removing all documents. The
remove
function removes three documents from the
collection.
collection = "employee"; n = remove(conn,collection,"{}")
n = 3
Close the MongoDB connection.
close(conn)
Input Arguments
conn
— MongoDB connection
mongo
object
MongoDB connection, specified as a mongo
object.
collection
— Collection name
string scalar
Collection name, specified as a string scalar.
Example: "taxidata"
Data Types: string
mongoquery
— MongoDB query
string scalar | character vector
MongoDB query, specified as a string scalar or character vector. Specify a JSON-style string to query the database.
Example: "{""department"":""Sales""}"
queries the database for documents
where the department
field is equal to
Sales
.
Example: "{""salary"": {$gt: 90000}}"
queries the database for documents
where the value of the salary
field is
greater than 90000
.
Data Types: char
| string
Output Arguments
n
— Number of documents removed
numeric scalar
Number of documents removed from a collection in the database, returned as a numeric scalar.
Version History
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)