[英]Access document of mongodb in javascript using node.js
我正在嘗試通過javascript函數訪問mongodb數據庫文檔,我可以使用commond提示符來訪問文檔。 我的要求是在Java腳本功能中執行相同的操作
db.products.find()[34];
這將給我這樣的回報
{
"_id" : ObjectId("5523c16d460df2542b8b56f4"),
"refProductId" : 75,
"name" : "3g%qg3O2Rd",
"SKUType" : ")pvvrYhebP",
"category" : "&aX96[u0Rn",
"subCat" : "Piutu Pike",
"price" : 175.6979,
"pkgType" : "q)Hr$(K*07",
"expDate" : ISODate("2015-06-22T05:59:17.844Z"),
"manfacDate" : ISODate("2015-11-19T04:30:58.347Z"),
"productSince" : ISODate("1989-11-24T07:29:28.753Z"),
"pkgLastChangedDate" : ISODate("2015-06-19T22:52:24.136Z"),
"mfId" : null
}
我想在我的java函數中實現相同的目的。 但是我總是變得不確定(我在查詢之后綁定了所有方法)
這是我的JavaScript代碼之一
// Connection URL
var url = 'mongodb://localhost:27017/test';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
var prodtcol=db.collection('products');
var prodtcol=db.collection('products');
console.log(prodtcol.find()[3]);
});
這是我在node.js上運行
嘗試以下方法:
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
var prodtcol=db.collection('products');
prodtcol.find(function(err, products){
// products is an array of products.
console.log(products); // Log all
console.log(products[3]); // Log the 4th product, where 0 is the first.
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.