package com.xusheng.nosql.redis; import java.util.Map; import redis.clients.jedis.Jedis; public class jedis_query { /** * @param xusheng */ public static void main(String[] args) { // TODO Auto-generated method stub Jedis jedis = new Jedis("localhost"); String value=jedis.hget("student.scofield", "English"); System.out.println("scofield's English score is: "+value); } } { “name”: “zhangsan”, “score”: { “English”: 69, “Math”: 86, “Computer”: 77 } } { “name”: “lisi”, “score”: { “English”: 55, “Math”: 100, “Computer”: 88 } } use student var stus=[ {"name":"zhangsan","scores":{"English":69,"Math":86,"Computer":77}}, {"name":"lisi","score":{"English":55,"Math":100,"Computer":88}} ] db.student.insert(stus) db.student.find().pretty() db.student.find({"name":"zhangsan"},{"_id":0,"name":0}) db.student.update({"name":"lisi"}, {"$set":{"score.Math":95}} ) { “name”: “scofield”, “score”: { “English”: 45, “Math”: 89, “Computer”: 100 } } package com.xusheng.nosql.MongoDB; import java.util.ArrayList; import java.util.List; import org.bson.Document; import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; public class mongo_insert { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //实例化一个mongo客户端 MongoClient mongoClient = new MongoClient("localhost", 27017); //实例化一个mongo数据库 MongoDatabase mongoDatabase = mongoClient.getDatabase("student"); //获取数据库中某个集合 MongoCollection<Document> collection = mongoDatabase.getCollection("student"); //实例化一个文档,内嵌一个子文档 Document document = new Document("name", "scofield"). append("score", new Document("English", 45). append("Math", 89). append("Computer", 100)); List<Document> documents = new ArrayList<Document>(); documents.add(document); //将文档插入集合中 collection.insertMany(documents); System.out.println("文档插入成功"); } } package com.xusheng.nosql.MongoDB; import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; import org.bson.Document; public class mongo_query { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //实例化一个mongo客户端 MongoClient mongoClient=new MongoClient("localhost",27017); //实例化一个mongo数据库 MongoDatabase mongoDatabase = mongoClient.getDatabase("student"); //获取数据库中某个集合 MongoCollection<Document> collection = mongoDatabase.getCollection("student"); //进行数据查找,查询条件为name=scofield, 对获取的结果集只显示score这个域 MongoCursor<Document> cursor=collection.find( new Document("name","scofield")). projection(new Document("score",1).append("_id", 0)).iterator(); while(cursor.hasNext()) System.out.println(cursor.next().toJson()); } }
标签:11,26,name,new,score,student,2023,import,com From: https://www.cnblogs.com/wllovelmbforever/p/17857988.html