简体   繁体   English

MongoDB,如何创建一个空集合?

[英]MongoDB, how to create an empty collection?

How can I create an empty collection in MongoDB? 如何在MongoDB中创建一个空集合?

I've tried various things but they didn't work, that would be declaring it differently. 我尝试过各种方法,但是它们没有用,那就是声明不同。 What I know is that it should create a collection automatically when needed, but not sure. 我知道的是,它应该在需要时自动创建一个集合,但不确定。

However, you could technically create a collection and delete that what is inside. 但是,您可以从技术上创建集合并删除其中的内容。

This is an example on what I've tried: 这是我尝试过的一个示例:

function test() {
        db.createCollection("test", function() {});
        console.log("Collection has been created.")
};
test();

The following creates a collection for me and prints your log message: 以下内容为我创建了一个集合并打印您的日志消息:

function test() {
        db.createCollection("test");
        console.log("Collection has been created.")
};
test();

I removed the function(){} in your db.createCollection, maybe that caused your error? 我在您的db.createCollection中删除了function(){},也许这引起了您的错误?

In MongoDB no need to explicitly check if a collection already exists or not before doing any CRUD operations. 在MongoDB中,无需在执行任何CRUD操作之前显式检查集合是否已存在。

MongoDB will create the collections on the when it is required. MongoDB将在需要时在上创建集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM