无法从mongo db获取数据

时间:2014-05-02 09:37:27

标签: node.js mongodb mongoose

我使用express来使服务听取使用mongoose从mogno db获取数据的请求。我的服务器index.js看起来像这样。

var express = require("express");
var contacts = require("./contact");
var app = express();

app.configure(function() {
    app.use(express.bodyParser());
});
app.get("/", contacts.index);
app.get('/contacts', contacts.index);
app.listen(1222, "127.0.0.1");

我从数据库中获取数据的contact.js文件结构如下:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mukund');

var Schema = mongoose.Schema;

var ContactSchema = new Schema({
    username: { type: String }
});

var ContactModel = mongoose.model('Contact', ContactSchema);

exports.index = function(req, res) {
    return ContactModel.find(function(err, contacts) {
        if (!err) {
            res.jsonp(contacts);
        } else {
            console.log(err);
        }
    });
}

问题是我有一个转储的mongo数据库,我想从转储的数据库中获取数据。但我不知道如何在没有我自己的模型的情况下获取数据。任何人都可以建议我使用mongoose获取数据的方法,而不知道模型。

我的数据的示例行如下所示:

    {
"__v" : 0,
"_id" : ObjectId("52cbd53612fd507e06005a6d"),
"activities" : [ ],
"admin" : false,
"created" : ISODate("2014-01-07T10:21:42.753Z"),
"email" : "gopal@mypromovideos.com",
"email_token" : "206140c1-f1f2-4f0c-9956-ea50445dd633",
"email_token_created" : ISODate("2014-01-07T10:21:42.753Z"),
"email_verified" : true,
"geo" : [
    76.98138157922307,
    11.01664471263072
],
"hash" : "$2a$10$.cV3FDS51XnxdiDcM2HJ5uhMANAwTudwDkucse4NToITYDWbkosca",
"last_activity" : ISODate("2014-01-07T10:31:17.508Z"),
"notifications" : [
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("53579f6e3c842fc05200344f"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("53579f6e3c842fc05200344f"),
        "_id" : ObjectId("53579f713c842fc052003462"),
        "private" : false,
        "created" : ISODate("2014-04-23T11:09:37.892Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("5357e94d3c842fc052003659"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("5357e94d3c842fc052003659"),
        "_id" : ObjectId("5357e9573c842fc05200366c"),
        "private" : false,
        "created" : ISODate("2014-04-23T16:24:55.563Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535912c03c842fc052003863"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535912c03c842fc052003863"),
        "_id" : ObjectId("535912c43c842fc052003876"),
        "private" : false,
        "created" : ISODate("2014-04-24T13:33:56.622Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535913783c842fc052003a70"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535913783c842fc052003a70"),
        "_id" : ObjectId("5359137b3c842fc052003a83"),
        "private" : false,
        "created" : ISODate("2014-04-24T13:36:59.271Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535a9c913c842fc052003c7d"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535a9c913c842fc052003c7d"),
        "_id" : ObjectId("535a9c943c842fc052003c90"),
        "private" : false,
        "created" : ISODate("2014-04-25T17:34:12.042Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535a9d693c842fc052003e8c"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535a9d693c842fc052003e8c"),
        "_id" : ObjectId("535a9d6b3c842fc052003e9f"),
        "private" : false,
        "created" : ISODate("2014-04-25T17:37:47.302Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535a9e5b3c842fc05200409b"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535a9e5b3c842fc05200409b"),
        "_id" : ObjectId("535a9e5e3c842fc0520040ae"),
        "private" : false,
        "created" : ISODate("2014-04-25T17:41:50.092Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535b505e3c842fc0520042aa"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535b505e3c842fc0520042aa"),
        "_id" : ObjectId("535b50613c842fc0520042bd"),
        "private" : false,
        "created" : ISODate("2014-04-26T06:21:21.156Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535bd5013c842fc0520044ba"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535bd5013c842fc0520044ba"),
        "_id" : ObjectId("535bd5053c842fc0520044cd"),
        "private" : false,
        "created" : ISODate("2014-04-26T15:47:17.370Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535c7a023c842fc0520046cb"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535c7a023c842fc0520046cb"),
        "_id" : ObjectId("535c7a043c842fc0520046de"),
        "private" : false,
        "created" : ISODate("2014-04-27T03:31:16.847Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535d08e53c842fc0520048de"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535d08e53c842fc0520048de"),
        "_id" : ObjectId("535d08e73c842fc0520048f1"),
        "private" : false,
        "created" : ISODate("2014-04-27T13:40:55.930Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535dbc0a3c842fc052004af2"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535dbc0a3c842fc052004af2"),
        "_id" : ObjectId("535dbc0d3c842fc052004b05"),
        "private" : false,
        "created" : ISODate("2014-04-28T02:25:17.238Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535de80c3c842fc052004d06"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535de80c3c842fc052004d06"),
        "_id" : ObjectId("535de80e3c842fc052004d19"),
        "private" : false,
        "created" : ISODate("2014-04-28T05:33:02.749Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535e12063c842fc052004f1a"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535e12063c842fc052004f1a"),
        "_id" : ObjectId("535e12093c842fc052004f2d"),
        "private" : false,
        "created" : ISODate("2014-04-28T08:32:09.718Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535e1e313c842fc05200512f"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535e1e313c842fc05200512f"),
        "_id" : ObjectId("535e1e333c842fc052005142"),
        "private" : false,
        "created" : ISODate("2014-04-28T09:24:03.045Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535f11a13c842fc052005344"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535f11a13c842fc052005344"),
        "_id" : ObjectId("535f11a43c842fc052005357"),
        "private" : false,
        "created" : ISODate("2014-04-29T02:42:44.388Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535f13273c842fc052005559"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535f13273c842fc052005559"),
        "_id" : ObjectId("535f13293c842fc05200556c"),
        "private" : false,
        "created" : ISODate("2014-04-29T02:49:13.563Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535f64b83c842fc052005771"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535f64b83c842fc052005771"),
        "_id" : ObjectId("535f64bb3c842fc052005784"),
        "private" : false,
        "created" : ISODate("2014-04-29T08:37:15.953Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("535ff59c3c842fc052005987"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("535ff59c3c842fc052005987"),
        "_id" : ObjectId("535ff59f3c842fc05200599a"),
        "private" : false,
        "created" : ISODate("2014-04-29T18:55:27.221Z")
    },
    {
        "type" : "Shout",
        "title" : "shouted",
        "type_id" : ObjectId("5360646c3c842fc052005ba1"),
        "sender" : ObjectId("52a6e799701054a10a000095"),
        "target" : ObjectId("5360646c3c842fc052005ba1"),
        "_id" : ObjectId("5360646f3c842fc052005bb4"),
        "private" : false,
        "created" : ISODate("2014-04-30T02:48:15.011Z")
    }
],
"online" : false,
"password_token" : "4aaf6362-b54a-44ee-a617-caf260675bc3",
"password_token_created" : ISODate("2014-01-07T10:21:42.753Z"),
"profile_image" : "http://lecafe.s3-website-ap-southeast-1.amazonaws.com/52cbd53612fd507e06005a6d.jpg?v=ebbfcf",
"tokens" : [ ],
"username" : "gopalg"
    }

0 个答案:

没有答案