博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mongodb和一些前端js的小方法笔记
阅读量:6912 次
发布时间:2019-06-27

本文共 4254 字,大约阅读时间需要 14 分钟。

判断客户端,然后打开已经安装的app (安卓系统下)

  1.  
    var isIDevicePhone = (/iphone|ipod/gi).test(navigator.userAgent);
  2.  
    var isIDeviceIpad = !isIDevicePhone && (/ipad/gi).test(navigator.userAgent);
  3.  
    var isIDevice = isIDevicePhone || isIDeviceIpad;
  4.  
    var isAndroid = !isIDevice && (/android/gi).test(navigator.userAgent);
  5.  
    var ua = navigator.userAgent.toLowerCase();
  6.  
    var isWeixin = ua.match(/micromessenger/gi);
  7.  
    if(isAndroid && !isWeixin) {
  8.  
    window.location="nihao://nihao";//打开某手机上的某个app应用
  9.  
    }
  10.  
     
  11.  
    <intent-filter>
  12.  
                    <action android:name="android.intent.action.VIEW" />
  13.  
                    <category android:name="android.intent.category.BROWSABLE" />
  14.  
                    <category android:name="android.intent.category.DEFAULT"/>
  15.  
                    <data android:scheme="nihao" android:pathPrefix="/nihao" />
  16.  
                </intent-filter>

mongodb的属性为对象的字段property,对property对象里再次添加一些属性,并将属性赋值

  • 遍历property对象的属性(名称为汉字的方法) property[item]
  • 根据_id查询条件修改property
  1.  
    var person = db.getCollection("").find({});
  2.  
     
  3.  
    person.forEach(
    function(event) {
  4.  
    print(event.variables.recordId +
    " "+ event.word);
  5.  
    var property;
  6.  
    db.getCollection(
    "").find({
    "recordId":NumberLong(event.variables.recordId)}).forEach(function(n) {
  7.  
    property = n.property;
  8.  
    property.recordId=event.variables.recordId;
  9.  
    property.rate=event.variables.rate;
  10.  
    property[
    "简介"] = n.introduction;
  11.  
    });
  12.  
     
  13.  
    for(var item in property) {
  14.  
    print(property[item]);
  15.  
    }
  16.  
    db.getCollection(
    "").update({
    '_id':event._id}, {
    $set:{
    'property':property, "del":"1"}})
  17.  
    });

将一个表的数据保存到另一张表中,相比较save和insert方法

  • insert: 若新增数据的主键_id已经存在,则会抛 org.springframework.dao.DuplicateKeyException 异常提示主键重复,不保存当前数据。
  • save: 若新增数据的主键_id已经存在,则会对当前已经存在的数据进行修改操作。
  1.  
    var notin = db.getCollection("").find({});
  2.  
     
  3.  
    notin.forEach(
    function (event) {
  4.  
    print(
    "notin_bak _id-->" + event._id + " word--> " + event.word);
  5.  
    db.getCollection(
    "").save(event);
  6.  
     
  7.  
    });

将一个表的数据保存到另一张表中,如何另一张表的word与一个表的word相同,就不保存。也就是保存在另一张表中不存在word的方法

  1.  
    var find = db.getCollection("").find({});
  2.  
     
  3.  
    find.forEach(
    function(event) {
  4.  
    var flag = true;
  5.  
    db.getCollection(
    "").find({
    "word":event.name}).forEach(function(n) {
  6.  
    flag =
    false;
  7.  
    db.getCollection(
    "").save(event);
  8.  
    });
  9.  
     
  10.  
    if(flag) {
  11.  
    db.getCollection(
    "").save(event);
  12.  
    }
  13.  
    });

查询属性不存在 [ "运动项目", "生肖", "星座", "血型"]的字段的方法 

indexof是判断一个数组是否含有某个元素的方法,不存在的返回-1

  1.  
     
  2.  
    var find = db.getCollection("").find({ "category": /.*人物.*/i });
  3.  
     
  4.  
    var persongArray = [ "运动项目", "生肖", "星座", "血型"];
  5.  
     
  6.  
     
  7.  
    find.forEach(
    function(event) {
  8.  
    print(
    "_id"+ event._id + " word " +event.word);
  9.  
    var errorcount = 0;
  10.  
    for(var item in event.variables) {
  11.  
    //词典表中的字段
  12.  
     
  13.  
    if(persongArray.indexOf(item) < 0) {
  14.  
    print(item);
  15.  
    errorcount++;
  16.  
    }
  17.  
    // print(persongArray.indexOf(item));
  18.  
    }
  19.  
    print(
    "不匹配字段数目:" + errorcount + "\n");
  20.  
    });

打开一个app,如果打不开则跳转到另一个页面

  1.  
    function isInstalled(){
  2.  
    /*var the_href="https://mobile.baidu.com/item?docid=24307148&source=pc";//获得下载链接
  3.  
    window.location="nihao://nihao";//打开某手机上的某个app应用,在intent-filter里面,由安卓客户端同事提供
  4.  
    setTimeout(function(){
  5.  
    window.location=the_href;//如果超时就跳转到app下载页
  6.  
    },500);*/
  7.  
    var loadDateTime = new Date();
  8.  
    window.location = "nihao://nihao"; //打开app的协议,在
  9.  
    setTimeout(
    function () {
  10.  
    var timeOutDateTime = new Date();
  11.  
    if (!loadDateTime || timeOutDateTime - loadDateTime < 1510) {
  12.  
    window.location = ""; //如果超时就跳转到app下载页
  13.  
    }
  14.  
    },
    1500);
  15.  
    }
  16.  
    <intent-filter>
  17.  
                    <action android:name="android.intent.action.VIEW" />
  18.  
                    <category android:name="android.intent.category.BROWSABLE" />
  19.  
                    <category android:name="android.intent.category.DEFAULT"/>
  20.  
                    <data android:scheme="nihao" android:pathPrefix="/nihao" />
  21.  
                </intent-filter>

 

jquery将一些题目打印出控制台中,类似于一个爬虫爬取内容

  1.  
    for(var i=2; i<=13;i++) {
  2.  
        
  3.  
        
    window.location.href="https://www.zhiliti.com.cn/html/changshiti/list22_"+i+".html"
  4.  
        
    var anniu;
  5.  
        
    for(var i=0; i<$(".list").find("li a").size(); i++) {
  6.  
            
    var titleStr = $(".list").find("li a").get(i).title;
  7.  
            $(
    ".list").find("li span").get(i).onclick();
  8.  
            
    var answerStr =  $(".anniu").get(i).innerHTML.replace("答案:","");
  9.  
            anniu +=
    "Q:" + titleStr +"\n A:"+ answerStr+"\n\n";
  10.  
        }
  11.  
        
    console.log(anniu);
  12.  
     
  13.  
    }

js将sring转为json    js将json转为string

  1.  
    var jsonobj = JSON.parse(str) //将string转为json
  2.  
     
  3.  
    var last=obj.toJSONString(); //将JSON对象转化为JSON字符
  4.  
    var last=JSON.stringify(obj); //将JSON对象转化为JSON字符

js将string转为int

发现parseInt方法在format'00'开头的数字时会当作2进制转10进制的方法进行转换,所以建议string转int最好用Number方法

  1.  
    parseInt(str) //将str的string类型转为int
  2.  
     
  3.  
    Number(str) //建议使用这种

js操作json对象,删除对象中一个属性,也就是删除一个key:value

delete singer.name //这样的话 singer中的 "name":"周杰伦" 这个键值对就被干掉了

转载于:https://www.cnblogs.com/TOLLA/p/9646922.html

你可能感兴趣的文章
JavaScript用JQuery呼叫Server端方法
查看>>
REST::Neo4p – PERL版本的”OGM”
查看>>
ORA-19808错误一例
查看>>
DirectShowNet 使用摄像头录像+录音
查看>>
Oracle、SQL Server、MySQL数据类型对比
查看>>
php开n次方
查看>>
eclipse自动补全的设置
查看>>
Delphi的三目运算 ifthen 和iif
查看>>
libcurl多线程超时设置不安全(转)
查看>>
3种web会话管理的方式
查看>>
Atitit 常用比较复杂的图像滤镜 attilax大总结
查看>>
ife任务刷题总结(一)-css reset与清除浮动
查看>>
JSContext
查看>>
字符识别(模板匹配&BP神经网络训练)
查看>>
【转】iOS 删除已经配置的类库和移除CocoaPods
查看>>
Python 列表
查看>>
如何限制用户在某一时间段多次访问接口
查看>>
Linux-4.4-x86_64 内核配置选项简介【转】
查看>>
linux中service *** start与直接运行/usr/bin/***的区别
查看>>
BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]
查看>>