[英]Mongoose Schema for remote MongoDb
I have opened a connection to my remote mongodb ec2 instance but now am trying to retrieve data that is nested within a collection. 我已经打开了到远程mongodb ec2实例的连接,但现在尝试检索嵌套在集合中的数据。 The database has multiple collections (ie visitor, campaign, form, etc...) and has data already in it from another source.
该数据库具有多个集合(即访问者,活动,表单等),并且已经有来自另一个来源的数据。 I am using node + express for the application.
我正在为应用程序使用node + express。
1) Do I have to define a schema in my app to match the remote database or can I just query for the data and store it in an object? 1)我必须在我的应用程序中定义一个架构来匹配远程数据库,还是可以只查询数据并将其存储在对象中? mongoose schema creation
猫鼬模式创建
2) Actually retrieving the values within the visitor collection, can I just use dot notation to query within the visitor
collection for visitor_id
using: 2)实际上在检索visitor集合中的值时,我可以使用点表示法来使用以下方法在
visitor
集合中查询visitor_id
:
db.find(visitor.visitor_id)
Here is the database connection code I am using if that helps 这是我正在使用的数据库连接代码,如果有帮助
var uri = 'mongodb://xx.xxx.xx.x'
var mongoOptions = { db: { safe: true } };
db = mongoose.createConnection(uri, mongoOptions, function (err, res) {
if (err) {
console.log('ERROR connecting to: remote' + uri + '. ' + err);
} else {
console.log('Successfully connected to: remote' + uri);
}
});
That notation won't work. 这种表示法不起作用。 If I understand the specific query you're trying to make (to fetch the document matching a
visitor_id
) then you'll need something roughly like this: 如果我了解您要进行的特定查询(以获取与
visitor_id
匹配的visitor_id
),那么您将大致需要以下内容:
// Assuming you already have mongoose connected to the database elsewhere var mongoose = require('mongoose'); var Schema = mongoose.Schema; var visitorSchema = new Schema({ visitor_id: Number, etc: etc // the rest of your schema }); var Visitor = mongoose.model('Visitor', visitorSchema); Visitor.findOne({ visitor_id: the_id_you_want_to_query }, function (err, doc) { // doc contains the visitor document, if found });
I suggest you familiarize yourself with queries with MongoDB and mongoose in particular—the docs aren't super easy to understand but cover most of the main cases. 我建议您熟悉一下MongoDB(尤其是mongoose)的查询-这些文档并不是很容易理解,但涵盖了大多数主要情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.