2017-02-14 54 views
0

我正在使用JavaScript中的MQTT。當用戶正在等待消息時,發佈者將每1毫秒發佈消息。在MongoDB中存儲多個JSON時出錯

但是在訂戶端,它收到消息,但在存儲到MongoDB時發生錯誤。

以下是我的代碼。 請幫我出來。提前致謝。

publisher.js

var mqtt = require('mqtt'); 
 
var url = 'mqtt://localhost:1883'; 
 

 
/*var payload = { 
 
    deviceId : '8675309' 
 
};*/ 
 

 
var client = mqtt.connect(url); 
 
var sensorProperty = {}; 
 
client.on('connect', function() { 
 
var publishedData = []; 
 
var totalNoData = 100000; 
 
    
 
setInterval(
 
     function() // Call out to get the time 
 
     { 
 
     \t console.log(publishedData.length); 
 
     if (publishedData.length < totalNoData) { 
 
     var sensorId = 4001; 
 
     \t var intervalId = setInterval(
 
\t  function(){ 
 
\t   if (sensorId < 9001) { 
 
\t   sensorProperty["dfsdffsdf"] = sensorId.toString(); 
 
     \t sensorProperty["accountId"] = "1388142455353"; 
 
     \t sensorProperty["dfgdfgf"] = 1; 
 
     \t sensorProperty["dfgdfgdfg"] = 260; 
 
     \t sensorProperty["podfgdfgdfgwer"] = 12; 
 
     \t sensorProperty["timestamp"] = new Date(); 
 
     \t sensorProperty.timestamp.setDate(sensorProperty.timestamp.getDate() - 50); 
 
     \t sensorProperty.timestamp = new Date(sensorProperty.timestamp).toISOString(); 
 
      sensorProperty.timestamp = new Date(sensorProperty.timestamp); 
 
     \t var message = JSON.stringify(sensorProperty); 
 
     \t console.log(message) 
 
\t   client.publish('airasoul', message, { qos: 1 }, function() { 
 
\t    
 
\t   }); 
 
\t   sensorId++; 
 
\t   } else { 
 
\t    clearInterval(intervalId); 
 
\t    console.log("cycle complete"); 
 
\t   } 
 

 
\t  },1) 
 
     }else { 
 
     \t clearInterval(); 
 
     \t console.log("Exceeds"); 
 
     } 
 
    }, 20000);// end check 
 

 

 
});

Subscriber.js

var mqtt = require('mqtt'); 
 
var url = 'mqtt://localhost:1883'; 
 
var MongoClient = require('mongodb').MongoClient; 
 
var assert = require('assert'); 
 
var ObjectId = require('mongodb').ObjectID; 
 
var mongoUrl = 'mongodb://localhost:27017/dfgdfgdfgdfgd'; 
 

 
var client = mqtt.connect(url, { clientId: 'mqtt-sub-', clean: false }); 
 
var receivedMessage = []; 
 
client.on('connect', function() { 
 
    client.subscribe('airasoul', { qos: 1 }); 
 
}); 
 

 
client.on('message', function (topic, message) { 
 
    receievedMessage = JSON.parse(message.toString()); 
 
    console.log('received message ', message.toString()); 
 
    MongoClient.connect(mongoUrl, function(err, db) { 
 
    \t assert.equal(err, null); 
 
\t insertDocument(db, function() { 
 
\t  db.close(); 
 
\t }); 
 
\t }); 
 
}); 
 
    
 
var insertDocument = function(db, callback) { 
 
    db.collection('sensordata').insertMany([receievedMessage], function(err, result) { 
 
\t  console.log("Inserted documents into the sensorData collection."); 
 
\t  console.log(err); 
 
\t  console.log(result); 
 
\t  callback(); 
 
\t }); 
 
};

錯誤它顯示,

[MongoError: E11000 duplicate key error collection: powerbankdb.sensordata index: _id_ dup key: { : ObjectId('58a2f56dd190242e789d758d') }] 
 
name: 'MongoError', 
 
message: 'E11000 duplicate key error collection: powerbankdb.sensordata index: _id_ dup key: { : ObjectId(\'58a2f56dd190242e789d758d\') }', 
 
driver: true, 
 
code: 11000, 
 
index: 0, 
 
errmsg: 'E11000 duplicate key error collection: powerbankdb.sensordata index: _id_ dup key: { : ObjectId(\'58a2f56dd190242e789d758d\') }', 
 
getOperation: [Function], 
 
toJSON: [Function], 
 
toString: [Function] } 
 
ok: [Getter],

回答

0

錯誤是說,存在重複鍵。確保沒有文件具有相同的_id(在這種情況下爲「58a2f56dd190242e789d758d」)

+0

id是mongo genereated no sir。 – Chandrashekhar