我在mongodb中有一个这样的数据

{ 
    "latitude" : "", 
    "longitude" : "", 
    "course" : "", 
    "battery" : "0", 
    "imei" : "0", 
    "altitude" : "F:3.82V", 
    "mcc" : "07", 
    "mnc" : "007B", 
    "lac" : "2A83", 
    "_id" : ObjectId("4f0eb2c406ab6a9d4d000003"), 
    "createdAt" : ISODate("2012-01-12T20:15:31Z") 
}

如何查询db.gpsdata。找到({“createdAt”:? ?这里是什么??}),以便它从db返回上面的数据结果给我?

如果您有子文档数组,Mongoose会自动为每个子文档数组创建id。例子:

{
    _id: "mainId"
    subDocArray: [
      {
        _id: "unwantedId",
        field: "value"
      },
      {
        _id: "unwantedId",
        field: "value"
      }
    ]
}

是否有一种方法告诉猫鼬不为数组中的对象创建id ?

我玩周围的MongoDB试图弄清楚如何做一个简单的

SELECT province, COUNT(*) FROM contest GROUP BY province

但是我似乎不能用聚合函数来算出来。我可以用一些奇怪的组语法来做

db.user.group({
    "key": {
        "province": true
    },
    "initial": {
        "count": 0
    },
    "reduce": function(obj, prev) {
        if (true != null) if (true instanceof Array) prev.count += true.length;
        else prev.count++;
    }
});

但是是否有更简单/更快的方法使用聚合函数?

我有一个基本的Node.js应用程序,我试图使用Express框架启动。我有一个views文件夹,其中有一个index.html文件。但是我在加载网页时收到以下错误:

Error: Cannot find module 'html'

下面是我的代码。

var express = require('express');
var app = express.createServer();

app.use(express.staticProvider(__dirname + '/public'));

app.get('/', function(req, res) {
    res.render('index.html');
});

app.listen(8080, '127.0.0.1')

我错过了什么?

你能分享一下你如何在MongoDB中实现数据版本控制的想法吗?(关于卡桑德拉,我也问过类似的问题。如果你有任何想法,哪个db更好,请分享)

假设我需要对一个简单地址簿中的记录进行版本化。(地址簿记录存储为平面json对象)。我希望历史:

是否会不经常使用 会被一次性用“时间机器”的方式呈现出来吗 一张唱片的版本不会超过几百个。 历史不会终结。

我正在考虑以下方法:

Create a new object collection to store history of records or changes to the records. It would store one object per version with a reference to the address book entry. Such records would looks as follows: { '_id': 'new id', 'user': user_id, 'timestamp': timestamp, 'address_book_id': 'id of the address book record' 'old_record': {'first_name': 'Jon', 'last_name':'Doe' ...} } This approach can be modified to store an array of versions per document. But this seems to be slower approach without any advantages. Store versions as serialized (JSON) object attached to address book entries. I'm not sure how to attach such objects to MongoDB documents. Perhaps as an array of strings. (Modelled after Simple Document Versioning with CouchDB)

我可以看到Docker占用了12GB的文件系统:

2.7G    /var/lib/docker/vfs/dir
2.7G    /var/lib/docker/vfs
2.8G    /var/lib/docker/devicemapper/mnt
6.3G    /var/lib/docker/devicemapper/devicemapper
9.1G    /var/lib/docker/devicemapper
12G     /var/lib/docker

但是,我怎么知道它是如何分布在容器上的呢?

我试图通过运行(新的v1.3命令)附加到容器

docker exec -it <container_name> bash

然后运行'df -h'来分析磁盘使用情况。它似乎是有效的,但不是使用“volumes-from”的容器。

例如,我为MongoDB使用了一个仅数据的容器,称为“MongoDB -data”。

当我运行docker运行-it——volumes-from mongo-data busybox,然后在容器内df -h,它说文件系统安装在/data/db(我的'mongo-data'数据容器)使用11.3G,但当我做du -h /data/db时,它说它只使用2.1G。

那么,如何分析容器/卷磁盘使用情况呢?或者,在我的情况下,我如何找到'mongo-data'容器大小?

下面是我的用户模式在user.js模型-

var userSchema = new mongoose.Schema({
    local: {
        name: { type: String },
        email : { type: String, require: true, unique: true },
        password: { type: String, require:true },
    },
    facebook: {
        id           : { type: String },
        token        : { type: String },
        email        : { type: String },
        name         : { type: String }
    }
});

var User = mongoose.model('User',userSchema);

module.exports = User;

这就是我在控制器中使用它的方式

var user = require('./../models/user.js');

这就是我在数据库中保存它的方式

user({'local.email' : req.body.email, 'local.password' : req.body.password}).save(function(err, result){
    if(err)
        res.send(err);
    else {
        console.log(result);
        req.session.user = result;
        res.send({"code":200,"message":"Record inserted successfully"});
    }
});

错误- - - - - -

{"name":"MongoError","code":11000,"err":"insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.users.$email_1  dup key: { : null }"} 

我检查了db集合,没有这样的重复条目存在,让我知道我做错了什么?

供您参考- req.body.email和req.body.password是抓取值。

我也检查了这篇文章,但没有帮助

如果我完全删除,然后它插入文档,否则它抛出错误“重复”错误,即使我在local.email中有一个条目

FBFriendModel.find({
    id: 333
}, function (err, docs) {
    docs.remove(); //Remove all the documents that match!
});

上面的方法似乎不管用。记录还在那里。

有人能修好吗?

我有一个_ids数组,我想相应地获取所有文档,最好的方法是什么?

就像……

// doesn't work ... of course ...

model.find({
    '_id' : [
        '4ed3ede8844f0f351100000c',
        '4ed3f117a844e0471100000d', 
        '4ed3f18132f50c491100000e'
    ]
}, function(err, docs){
    console.log(docs);
});

该数组可能包含数百个_id。

我使用node-mongodb-native驱动与MongoDB写一个网站。

我有一些关于如何管理连接的问题:

Is it enough using only one MongoDB connection for all requests? Are there any performance issues? If not, can I setup a global connection to use in the whole application? If not, is it good if I open a new connection when request arrives, and close it when handled the request? Is it expensive to open and close a connection? Should I use a global connection pool? I hear the driver has a native connection pool. Is it a good choice? If I use a connection pool, how many connections should be used? Are there other things I should notice?