'use strict';
// connect MongoDB
var mongodb = require("mongodb");
var server = new mongodb.Server("127.0.0.1", 27017, {auto_reconnect:true});
var client = new mongodb.Db("testDB", server, {w:1});
client.open(function(err, db){
if (err)
{
throw err;
}
else
{
db.collection("testTable", {safe:true}, function(err, collection){
if (err)
{
throw err;
}
else
{
// 全部查找
collection.find().toArray(function(err, result){
//JsonLog(result);
});
// 创建单个索引 1表示对应键的索引按升序存储,-1表示对应键的索引按降序存储。
//collection.ensureIndex({name:1}, {}, function(err, result){
// JsonLog(result);
//});
// 创建联合索引 1表示对应键的索引按升序存储,-1表示对应键的索引按降序存储。
//collection.ensureIndex({name:1, age:-1}, {}, function(err, result){
// JsonLog(result);
//});
// 创建索引,并且指定名称
//collection.ensureIndex({level:1}, {level:"x_level"}, function(err, result){
// JsonLog(result);
//});
// 创建唯一索引
//collection.ensureIndex({name:1}, {unique:true}, function(err, result){
// JsonLog(result);
//});
// 查看表的对应字段索引
//collection.indexInformation({age:1}, function(err, result){
// JsonLog(result);
//});
//// 删除表的对应字段索引
//collection.dropIndex({name:1}, {}, function(err, result){
// JsonLog(result);
//});
}
});
}
});
MongoDB_索引
最新推荐文章于 2025-06-22 08:46:07 发布