我在MongoDB中收集了以下表格.

[
    {
        "id" : 1,
    },

    {
        "id" : 2,
    },

    {
       "id" : 3
       "xid": 300
    }
]

I want to create a mongoexport statement that exports to a csv only documents with id's AND xid's with the value of xid > 0

我目前有以下命令:

mongoexport -h host -u user -p pass --db database --collection collections --csv --fields id,xid --query '{"xid":{"$ne":0}}' --out rec.csv

但是,这也会导出id不带xid的文档.所以我得到了

xid, id
12, 3
,4
14, 5
,3
,2
12, 5

有没有办法导出同时具有id和xid的文档?

推荐答案

您可以使用以下内容作为查询:

{ $and: [
    {xid: {$exists:true}},
    {xid:{$gt:0}}
    ]  
}

$and"对包含两个或多个表达式(例如,等)的数组执行逻辑"与"运算,并 Select 满足数组中所有表达式的文档."

$exists判断文档中是否存在特定字段.

Also, as it is being pointed out by Vijay, in your description you mention you want xid > 0. You shouldn't be using $ne (not equal), but $gt (greater than).

Mongodb相关问答推荐

在MogoDB中按时间间隔分组、统计文档和获取间隔时间

MongoDB合并按键更新值的对象数组

在提供的文档(_Id)之后和之前,是否有一个Mongo操作来获取已排序(和/或过滤)集合中的文档计数?

MongoDB Aggregate 根据时间进行多重分组

如何在 kubernetes 中获取分片 mongodb 的备份

MongoDB 更新:如果新值不同,则将旧字段值推送到另一个数组字段

在 MongoDB 中,如何使用对当前值进行操作的函数来更新嵌套文档的值?

PHP 无法加载动态库 (mongo.so)

MongoDB:插入重复键更新

根据 Month 删除 mongodb 中的旧记录

是否有适用于 Linux 的 MongoDB GUI 桌面应用程序?

MongoDB $or 查询

在 MongoDB 中合并两个集合

从 id 删除 PyMongo 中的文档

docker 在不同的端口上运行 mongo 映像

使用 Java 驱动程序更新 MongoDB 中的数组

mongoose:按字母顺序排序

MongoTemplate upsert - 从 pojo 进行更新的简单方法(哪个用户已编辑)?

Mongoose 为所有嵌套对象添加 _id

我可以只获取 Cursor 对象(pymongo)中的第一项吗?