Commit b2adb48b authored by Kapilraj Thangeswaran's avatar Kapilraj Thangeswaran

Ajout fonction mise à jour et suppression

parent c5c335c8
...@@ -33,7 +33,7 @@ Use "config.json" in the "mongo" folder to specify : ...@@ -33,7 +33,7 @@ Use "config.json" in the "mongo" folder to specify :
- database : the database you are using - database : the database you are using
- collection : the collection you are using - collection : the collection you are using
- request : the request you want to perform (for allowed requests, please check "Supported requests") - request : the request you want to perform (for allowed requests, please check "Supported requests")
```javascript ```
{ {
"url" : "mongodb://localhost:27017/", "url" : "mongodb://localhost:27017/",
"database" : "database", "database" : "database",
...@@ -44,13 +44,11 @@ Use "config.json" in the "mongo" folder to specify : ...@@ -44,13 +44,11 @@ Use "config.json" in the "mongo" folder to specify :
### In "web" folder ### In "web" folder
Use "config.json" in the "web" folder to specify : Use "config.json" in the "web" folder to specify :
- url : mongodb's url - url : mongodb's url including the database
- database : the database you are using
- collection : the collection you are using - collection : the collection you are using
```javascript ```
{ {
"url" : "mongodb://localhost:27017/", "url" : "mongodb://localhost:27017/database",
"database" : "database",
"collection" : "collection", "collection" : "collection",
} }
``` ```
......
<project name="hdoc_to_mongo" default="insertion"> <project name="hdoc_to_mongo" default="mongoDB">
<property file="build.properties"/> <property file="build.properties"/>
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
<delete dir="${tmpdir}" /> <delete dir="${tmpdir}" />
</target> </target>
<target name="insertion" depends="main"> <target name="mongoDB" depends="main">
<exec executable="node" dir="mongo"> <exec executable="node" dir="mongo">
<arg line="main.js"/> <arg line="main.js"/>
</exec> </exec>
......
...@@ -2,5 +2,5 @@ ...@@ -2,5 +2,5 @@
"url" : "mongodb://localhost:27017/", "url" : "mongodb://localhost:27017/",
"database" : "test", "database" : "test",
"collection" : "nf29", "collection" : "nf29",
"request" : "insert" "request" : "update"
} }
\ No newline at end of file
...@@ -11,20 +11,46 @@ if(url.slice(-1) == "/") { ...@@ -11,20 +11,46 @@ if(url.slice(-1) == "/") {
} }
const outputFolder = "../output"; const outputFolder = "../output";
var insertDocument = function(db, collection, json, callback) { var insertDocument = function(db, collection, json) {
db.collection(collection).insertOne(json, function(err, result) { db.collection(collection).insertOne(json, function(err, result) {
assert.equal(err, null); assert.equal(err, null);
console.log(json.title + " inserted into " + collection + " collection."); console.log(json.title + " inserted into " + collection + " collection.");
callback(); });
}); };
var updateDocument = function(db, collection, json) {
db.collection(collection).updateOne({"title" : json.title}, json, function(err, result) {
assert.equal(err, null);
if(result.result.n > 0) {
console.log(json.title + " updated.");
} else {
console.log(json.title + " not found.");
}
});
};
var removeDocument = function(db, collection, json) {
db.collection(collection).remove({"title" : json.title}, function(err, result) {
assert.equal(err, null);
if(result.result.n > 0) {
console.log(json.title + " removed.");
} else {
console.log(json.title + " not found.");
}
});
}; };
MongoClient.connect(url, function(err, db) { MongoClient.connect(url, function(err, db) {
assert.equal(null, err); assert.equal(null, err);
fs.readdir(outputFolder, (err, files) => { fs.readdir(outputFolder, (err, files) => {
files.forEach(file => { files.forEach(file => {
var json = JSON.parse(fs.readFileSync(outputFolder + "/" + file));
if(config.request === 'insert') { if(config.request === 'insert') {
insertDocument(db, config.collection, JSON.parse(fs.readFileSync(outputFolder + "/" + file)), function(){}); insertDocument(db, config.collection, json);
} else if(config.request === 'update') {
updateDocument(db, config.collection, json);
} else if(config.request === 'remove') {
removeDocument(db, config.collection, json);
} }
}); });
db.close(); db.close();
......
{ {
"url" : "http://172.25.31.252:28017/test", "url" : "http://localhost:28017/test",
"collection" : "nf29" "collection" : "nf29"
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment