Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MongoDB

MongoDB พิมพ์ JSON แบบไม่มี Whitespace (Unpretty JSON)

พิมพ์ JSON แบบบีบอัด (Single Line) ด้วย printjsononeline()

รูปแบบคำสั่ง

var yourVariableName = db.yourCollectionName.find().sort({_id:-1}).limit(10000);
while(yourVariableName.hasNext()) {
  printjsononeline(yourVariableName.next());
};

สร้างข้อมูลตัวอย่าง

> db.unprettyJsonDemo.insertOne({"StudentName":"John","StudentAge":21,"StudentTechnicalSkills":["C","C++"]});
{ "acknowledged" : true, "insertedId" : ObjectId("5c900df25705caea966c557d") }
> db.unprettyJsonDemo.insertOne({"StudentName":"Carol","StudentAge":22,"StudentTechnicalSkills":["MongoDB","MySQL"]});
{ "acknowledged" : true, "insertedId" : ObjectId("5c900e085705caea966c557e") }

พิมพ์แบบ Unpretty (บรรทัดเดียว)

> var myCursor = db.unprettyJsonDemo.find().sort({_id:-1}).limit(10000);
> while(myCursor.hasNext()){
  printjsononeline(myCursor.next());
};
{ "_id" : ObjectId("5c900e085705caea966c557e"), "StudentName" : "Carol", "StudentAge" : 22, "StudentTechnicalSkills" : [ "MongoDB", "MySQL" ] }
{ "_id" : ObjectId("5c900df25705caea966c557d"), "StudentName" : "John", "StudentAge" : 21, "StudentTechnicalSkills" : [ "C", "C++" ] }

เหมาะสำหรับ Log หรือส่งต่อไปยังเครื่องมืออื่นที่ต้องการ JSON บรรทัดเดียว