简体   繁体   中英

collection creation in mongoDB may cause some conflicts?

Assume $db is a made mongoDB connection with php.

There is no collection exists yet.

I inserted one data

$db->board_name->board->article->insert(array("title"=>"example"));

this is equivalent to

db.board_name.board.article.insert({"title":"example"})

the problem is it inserts not the

db.board_name.board

structure but

just simply inserts "db.board_name.board.article" <--- these three as a string.

Does it just demonstrates like a combined string for now

because there is no other elements after "board_name"?

What if I add more things right after "board_name" like

db.board_name.rank.insert({"something":"thing"});

Does mongoDB smartly recognize "rank" collection as part of "board_name" in that case?

or simply creates other collection with "db.board_name.rank"?

I'm not sure if I understand correctly, but what I think you're asking is, "why is there no collection named board_name.name "? That's because you haven't created one yet.

Each collection in MongoDB simply has a name. The fact that human beings (and drivers) use . s to separate the names into logical namespaces is a convenience to make it easier to use, and to let you logically group related collections, but from MongoDB's perspective, the name is simply a string.

Once you've created board_name.name.article , if you issue a subsequent insert or save operation to another collection, say, board_name.rank , then that collection will be created for you at that time. There will be no conflicts, your insert, update, or save operation will either identify a collection which already exists, and operate on that, or create a new collection and operate on that.

For more on how collections work, see the MongoDB docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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