将MongoDB Collection ObjectId转换为_id" string"

时间:2016-01-14 23:27:47

标签: javascript mongodb database

是否可以批量更新_id字段中包含_id :ObjectId("22332")的mongo集合为字符串,因此使用_id : "22332"

我正在使用Meteor,我已经使用mongoimport导入了csv,但它自动将ObjectId添加为(" 22332")我不想破坏默认的流星_id功能并且必须放{每个javascript文件都有{1}}。

._str

我想要这个

{
    "_id" : ObjectId("5696cf153f8d6d60f723268b"),
    "NTitle" : 18,
    "field1" : "company announces Private Placement",
    "field2" : "111099432820040602PP.pdf",
    "field3" : "Vancouver, BC, June 2, 2004...... ",
    "field4" : "2004-02-06",
    "field5" : "YES"
}

这是一个插入脚本。

{
    "_id" : 5696cf153f8d6d60f723268b,
    "NTitle" : 18,
    "field1" : "company announces Private Placement",
    "field2" : "111099432820040602PP.pdf",
    "field3" : "Vancouver, BC, June 2, 2004...... ",
    "field4" : "2004-02-06",
    "field5" : "YES"
}

1 个答案:

答案 0 :(得分:2)

这是解决我问题的一个粗略示例。

从phpmyadmin导出csv

在本地安装mongodb

运行

mongoimport -h localhost:3001 --db meteor --collection news4 --type csv --file env_news.csv --fields NID,NTitle,NPDF,NDesc,NAdded,NActive

服务器/ changeData.js

var test = News4.find().fetch();
var test1 = News4Final.find().fetch();

for (i = 0; i < test.length; i++){

News4Final.insert({_id : test[i]._id._str}); //Run this Firsttime.

//Run everything below  after first run

//var submitted = new Date(test[i].NAdded);

/*News4Final.update(test1[i]._id, {$set: {
title: test[i].NTitle,
PDF : test[i].NPDF,
postContent : test[i].NDesc,
submitted : submitted

}},{multi: false});*/


}

Dumb local Databse

mongodump --port 3001

senddatabase upto compose.io mongodb server。

mongorestore --host caneww.60.mon342.com:10654 --db enwave -u admin -p password dump/meteor

使用meteor up并在环境变量中设置数据库。同时在mup.json

中关闭mongodb setup为false
"MONGO_URL": "mongodb://admin:password@cand4ate.40.mongolayer.com:10644,candidate.12.mongolayer.com:11274/enwdve?replicaSet=set-569886d8c55a7400102b",
"MONGO_OPLOG_URL": "mongodb://admin:password@candi4te.40.mongolayer.com:10254,candidate.12.mongolayer.com:1d274/local?authSource=enwddve"