mongod操作问题。
发布于 8 年前 作者 xuzaixian 2799 次浏览 来自 分享

··· "_id": ObjectId(“50d3fdc6e4b024a11e8234c6”), “userId”: 280, “data”: [ { “name”: “test1”, “location”: “北京”, }, { “name”: “test1”, “location”: “北京 海淀区”, }, { “name”: “test3”, “location”: “河北 唐山”, } ] ··· 如果我想找到所有test:1的文档 就是结果如下所示,应该怎么做…第一次玩NOSQL玩不太转… ··· { “name”: “test1”, “location”: “北京”, }, { “name”: “test1”, “location”: “北京 海淀区”, } ···

2 回复

Message.getAll= function (name, callback) { // open db mongodb.open(function (err, db) { if (err) { return callback(err); }

    db.collection('messages', function (err, collection) {
        if (err) {
            mongodb.close();
            return callback(err);
        }
        var query = {};
        if (name) {
            query.name = name;
        }
        collection.count(query, function (err, total) {
            if (err) {
                return callback(err);
            }
            collection.find(query).sort({
                time: -1
            }).toArray(function (err, docs) {
                mongodb.close();
                if (err) {
                    return callback(err);
                }
                return callback(null, docs, total);
            });
        });
    });
});

}; Message接口名 messages数据库名 total数量

db.集合名.find({
  "data.name": "test1",
});
---------------------
比如:
db.orders.find({
  "amount": 10,
});
回到顶部