remove
Description
returns the number of documents removed from a collection using the MongoDB® C++ interface connection. Use a MongoDB query to specify removing one or multiple documents.n
= remove(conn
,collection
,mongoquery
)
Examples
Connect to MongoDB® using the MongoDB C++ interface and remove documents from a collection. Specify a MongoDB query to determine which documents to remove. In this example, the collection represents employee data.
Create a MongoDB connection to the database mongotest
using the MongoDB C++ interface. Here, the database server dbtb01
hosts this database using port number 27017
.
server = "dbtb01"; port = 27017; dbname = "mongotest"; conn = mongoc(server,port,dbname)
conn = connection with properties:
Database: "mongotest"
UserName: ""
Server: "dbtb01"
Port: 27017
CollectionNames: [14×1 string]
conn
is the connection
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 14 document collections.
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. For this example, specify the employeedata
collection. Create the MongoDB query to identify documents in the department that has the department identifier set to 80
.
collection = "employeedata"; mongoquery = "{""department_id"":80}";
Remove documents using the MongoDB query. The remove
function removes three documents from the collection.
n = remove(conn,collection,mongoquery)
n = int64
3
Close the MongoDB connection.
close(conn)
Connect to MongoDB® using the MongoDB C++ interface and remove all documents from a collection. In this example, the collection represents employee data.
Create a MongoDB connection to the database mongotest
using the MongoDB C++ interface. Here, the database server dbtb01
hosts this database using port number 27017
.
server = "dbtb01"; port = 27017; dbname = "mongotest"; conn = mongoc(server,port,dbname)
conn = connection with properties:
Database: "mongotest"
UserName: ""
Server: "dbtb01"
Port: 27017
CollectionNames: [14×1 string]
conn
is the connection
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 14 document collections.
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 employeedata
collection. Use an empty MongoDB query to specify removing all documents. The remove
function removes three documents from the collection.
collection = "employeedata"; n = remove(conn,collection,"{}")
n = int64
3
Close the MongoDB connection.
close(conn)
Input Arguments
MongoDB C++ interface connection, specified as a connection
object.
Collection name, specified as a string scalar.
Example: "taxidata"
Data Types: string
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: string
| char
Output Arguments
Number of documents removed from a collection in the database, returned as a numeric scalar.
Version History
Introduced in R2021b
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.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)