简体   繁体   English

MongoDB使用集合类型保存-另一种方法?

[英]MongoDB save with a collection type - another way?

First time using mongodb. 第一次使用mongodb。 I am using node.js and mongojs on the server side. 我在服务器端使用node.js和mongojs。 A little confused. 有点困惑。 The reversal of db.save is find, and I'd like to be able to do something similar to: 找到了db.save的逆向,我希望能够做类似的事情:

 db.furni.find( db.furni_types.chair, db.furni_types.chair.plastic );

Current setup: 当前设置:

// Chair types
db.furni_types.save({ type: "chair", material: "wood" });
db.furni_types.save({ type: "chair", material: "plastic" });
db.furni_types.save({ type: "chair", material: "beanbag" });

// other furni types
db.furni_types.save({ type: "bed", material: "silk" });
db.furni_types.save({ type: "table", effect: "glass" });


db.furni.save({id: "13626450", name: "ComfyMan", type: "chair", material: "wood", description: "The most comfortable chair in extistence."});

A few questions: 几个问题:

Am I going about this the right way or what? 我要以正确的方式还是什么方式?

Would the array found in db.furni.save(type: "chair", material: "wood") sufficient if I wanted to use these as objects later? 如果我以后想将它们用作对象,在db.furni.save(type:“ chair”,material:“ wood”)中找到的数组是否足够?

What if I wanted to do something like: 如果我想做类似的事情怎么办:

 db.furni.save( name: "Super Comfort", db.furni_types.chair, db.furni_types.chair.wood );

Would the above work? 以上工作吗?

This is definitely not the right way... First off it is very much a RDBMS way of thinking, (relational database...). 这绝对不是正确的方法...首先,它非常是RDBMS的思维方式(关系数据库...)。 Secondly if you want to be able to do something like this: 其次,如果您希望能够执行以下操作:

db.furni.find( db.furni_types.chair, db.furni_types.chair.plastic );

Then it would just be: 然后就是:

db.furni.find({type: "chair", material:"plastic"});

Note that what you are passing into find is an object, so you cannot just expect to pass strings or something from other variables in a comma separated format, let alone think that you can just reference your types model that way (db.furni_types != a direct accessor at the data in the database, its an interface upon which you have to "find"). 请注意,传递给find的是一个对象,因此您不能仅期望以逗号分隔的格式传递字符串或其他变量中的某些东西,更不用说以这种方式引用您的类型模型了(db.furni_types!=数据库中数据的直接访问者,它是您必须在其上“查找”的接口)。

So just looking at what you have posted and what I suggest, you will also see that if you have to type "chair" in both, and "plastic" in both regardless, what value do you expect to gain from your proposed approach? 因此,仅查看您发布的内容和我的建议,您还将看到,如果无论是在两者中都键入“ chair”,还是在两者中都键入“ plastic”,那么从您的提议方法中希望获得什么价值?

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

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