官方教程:https://docs.mongodb.com/manual/reference/method/
1、连接数据库
db = connect("localhost:27017"); // 连接数据库
db = db.getSiblingDB("victory"); // 选择 victory 数据库以上代码和下面一行用法相同
db = connect("localhost:27017/victory","用户名","密码"); // 连接数据库2、遍历输出集合数据
第一种方式:使用游标
var cursor = db.channel.find(); // 查询集合
while( cursor.hasNext() ){ // 迭代输出结果
printjson( cursor.next() );
}
// 关闭游标
cursor.close();第二种方式:使用forEach
use victory;
print("选择数据库 victory ");
遍历输出结果
db.channel.find().forEach(
function(item){
printjson(item);
}
);注:printjson(obj)与print(tojson(obj)) 用法一样
3、修改Collection
db.channel.updateMany({},{$set:{"priority":1}})观察到,数据其实保存到是Double类型,并非我所需要的Int32类型。
修改类型如下,增加一个NumberInt转换
db.channel.updateMany({},{$set:{"priority":NumberInt(1)}})// 替换,替换操作,没有$set,更新某个字段值,需要加上$set
db.article.update({"_id":articleId},{"priority":priority});// 更新
db.article.update({"_id":articleId},{$set:{"priority":priority}});完整实例
有4张表,Article文张表、articleChannelRel文章频道关联表、channel频道表、headlineArticle头条文章表,现在articleChannelRel和channel表要废弃,需要将“推荐”频道的文章关联的信息,记录到article表中。
articleChannelRel 表中有 priority、fixPosition两个字段,
priority:表示重要度1(普通)、2(醒目)、3(头条文章,对应headlineArticle表数据)
fixPosition:表示固定文章
操作步骤
1、连接数据库
2、查找channel表的推荐频道id
3、查找articleChannelRel表的文章关联信息,取出关联的重要度,根据重要度进行记录对应的article和headlineArticle,取出固定文章的文章id,设置article表。
/**---------------------------- 连接数据库 ----------------------------------------**/
db;
use victory;
print("选择数据库 victory ");
//db = connect("localhost:27017"); // 连接数据库
//db = db.getSiblingDB("victory"); // 选择 victory 数据库
db = connect("远程IP:端口/victory","用户名","密码"); // 连接数据库
/**---------------------------------------------------------------------------------**/
// 文章总数
db.article.count();
// 频道推荐关联表【醒目类型】总数
db.articleChannelRel.find({
"channelId":"1b5d3062-f479-4ff5-b91e-d51093a9d116",
"priority":2
}).count();
// 频道推荐关联表【醒目和头条类型】总数
db.articleChannelRel.find({"channelId":"xxxx-xxxx-xxxx-xxxx-xxxx","priority":{"$gt":1}},{creationDate:0,lastModifiedDate:0}).count();
/**----------------------------------------------------------------------------**/
/**
* 生成常用的UUID,Mongo默认的UUID方法不满足常规需求
*/
function ToGUID(hex) {
var a = hex.substr(6, 2) + hex.substr(4, 2) + hex.substr(2, 2) + hex.substr(0, 2);
var b = hex.substr(10, 2) + hex.substr(8, 2);
var c = hex.substr(14, 2) + hex.substr(12, 2);
var d = hex.substr(16, 16);
hex = a + b + c + d;
var uuid = hex.substr(0, 8) + '-' + hex.substr(8, 4) + '-' + hex.substr(12, 4) + '-' + hex.substr(16, 4) + '-' + hex.substr(20, 12);
return uuid;
}
/**
* 保存头条数据到 HeadlineArticle
*/
function saveHeadlineArticle(articleId){
// 查询 article 文章详情
db.article.find({"_id":articleId},{title:1, status:1}).forEach(
function(item){
var title = item.title;
var status = item.status;
var id = ToGUID(new BinData(3, ObjectId().str).hex());
// 查询头条文章是否存在
var count = db.headlineArticle.find({"articleId":articleId}).count();
if(count > 0){
print("头条文章 headlineArticle 已存在:", articleId);
return;
}
// 插入 headlineArticle 头条文章
var obj = {"_id":id, "articleId":articleId, "sequence":NumberInt(1), "articleTitle":title, "status": NumberInt(status), "createDate": new Date(), "modifyDate": new Date()};
db.headlineArticle.insert(obj);
print("【头条】往 headlineArticle 中插入一条头条数据", tojson(obj));
}
);
}
// 1、article 表所有文章 priority 设置为 1
db.article.updateMany({},{$set:{"priority":NumberInt(1)}});
// 2、删除固定文章 fixPosition 字段
db.article.updateMany({},{$unset:{"fixPosition":false}});
var tjId; // 推荐Id
// 3、读channel表,找到channelName=“推荐”的id
var channelCursor = db.channel.find({"channelName":"推荐"},{_id:1,channelName:1});
if(channelCursor.hasNext()){
var obj = channelCursor.next();
tjId = obj._id;
print("找到推荐Id:", tjId);
}else{
print("没有找到推荐ID");
}
// 关闭游标
channelCursor.close();
// 4、读articleChannelRel表channelId=id的列表
var articleChannelRelCursor = db.articleChannelRel.find({"channelId":tjId},{creationDate:0,lastModifiedDate:0});
while(articleChannelRelCursor.hasNext()){
var rel = articleChannelRelCursor.next();
var articleId = rel.articleId;
var priority = rel.priority;
var fixPosition = rel.fixPosition==undefined?"":rel.fixPosition;
// print("文章id:" + articleId + ", 重要度:" + priority + ",是否固定:" + fixPosition);
// 5、重新设置 article 重要度
if(priority > 1){
print("[重新设置文章 " + articleId + " 重要度(" + priority + ")]");
db.article.update({"_id":articleId},{$set:{"priority":NumberInt(priority)}});
if(priority == 3){
print("【头条】往 headlineArticle 中插入一条头条 " + articleId + " 数据");
// 保存头条数据到 headlineArticle
saveHeadlineArticle(articleId);
}
}
// 6、重新设置 article 固定文章
if(fixPosition == true){
print("【固定】重新设置文章 " + articleId + " 为固定文章");
db.article.update({"_id":articleId},{$set:{"fixPosition":true}});
}
}
// 关闭游标
articleChannelRelCursor.close();
// 7、已有头条文章headline
var headlineArticleCursor = db.headlineArticle.find({},{creationDate:0, lastModifiedDate:0});
while(headlineArticleCursor.hasNext()){
var headline = headlineArticleCursor.next();
var articleId = headline.articleId;
print("【headlineArticle】重新设置article文章 " + articleId + " 为头条文章");
// 重置article文章头条数据
db.article.update({"_id":articleId},{$set:{"priority":NumberInt(3)}});
}
// 关闭游标
headlineArticleCursor.close();未经允许请勿转载:程序喵 » MongoDB Shell 操作示例
程序喵