mongodb如何动态创建数据库/集合 [英] How does mongodb create database/collection on the fly
问题描述
如果我们运行类似于
db.store.save({a: 789});
它会自动创建 store
集合并添加一个记录它。
It automatically creates store
collection and add a document to it.
我的javascript理解说,无法在 db
的未定义属性上调用方法宾语。它应该会导致某种错误/异常。
My javascript understanding says it is not possible to call a method on an undefined property of db
object. It should have resulted in some kind of error/exception.
我很想知道幕后发生的事情,如果有任何有用的链接,请指出那些。谷歌搜索没有帮助我。
I am curious to understand the happenings behind the scene and if there is any helpful link please point me to those. Googling did not help me much.
推荐答案
我记得在NodeJS环境中你必须做这样的事情来实际创建一个记录: db.get('collectionName')。insert({.. something ...});
或 db.get('collectionName') .save({... something ...});
但你不能将集合名称用作db的属性。
As I recall in a NodeJS environment you must do something like this to actually create a record: db.get('collectionName').insert({..something...});
or db.get('collectionName').save({...something...});
but you don't get to use the collection name as a property of db.
您提到的行仅用于MongoDB shell,而不是Javascript。
我猜你误解了什么是MongoDB shell以及什么是MongoDB驱动程序。
The line you're mentioning is only used in MongoDB shell, which is not Javascript. I guess you're misunderstanding what's MongoDB shell and what's a MongoDB driver.
长篇故事MongoDB(驱动程序)无法访问未定义的属性。
So long story short MongoDB (driver) is not able to access an undefined property.
编辑
回复你的评论..
MongoDB JS驱动程序的GitHub页面几乎指出如何插入字段并始终使用我提到的语法: https://github.com/mongodb/node-mongodb-native
MongoDB JS driver's GitHub page pretty much points out how to insert a field and always uses the syntax I mentioned: https://github.com/mongodb/node-mongodb-native
至于你使用的是什么在shell中,很明显你不能只在命令shell中使用Javascript。所以我想我会指出一个你可以看到MongoDB开发的语言的地方: http:// www .mongodb.org / 几乎第一行说它是用C ++编写的。
As for what you're using in the shell it's pretty clear that you can't just use Javascript in a command shell. So I guess I'll point you to a place in which you can see in what language was MongoDB developed: http://www.mongodb.org/ pretty much the first line says it's written in C++.
希望这有助于澄清你的问题
Hope this helps clarify your question
这篇关于mongodb如何动态创建数据库/集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!