private void dbRefToRelation(DBObject dbObject) {
if (dbObject == null) {
return;
}
if (dbObject.containsField("_id"))
dbObject.put("_id", ((ObjectId) dbObject.get("_id")).toHexString());
for (String key : dbObject.keySet()) {
Object obj = dbObject.get(key);
if (obj instanceof DBRef) {
DBRef ref = (DBRef) obj;
dbObject.put(key, dbRefToRel(ref));
} else if (obj instanceof DBObject) {
dbRefToRelation((DBObject) obj);
}
}
}
java类com.mongodb.DBRef的实例源码
EntityDataController.java 文件源码
项目:restfiddle
阅读 113
收藏 0
点赞 0
评论 0
EntitySessionController.java 文件源码
项目:restfiddle
阅读 40
收藏 0
点赞 0
评论 0
private void dbRefToRelation(DBObject dbObject) {
if (dbObject == null) {
return;
}
if (dbObject.containsField("_id"))
dbObject.put("_id", ((ObjectId) dbObject.get("_id")).toHexString());
for (String key : dbObject.keySet()) {
Object obj = dbObject.get(key);
if (obj instanceof DBRef) {
DBRef ref = (DBRef) obj;
dbObject.put(key, dbRefToRel(ref));
} else if (obj instanceof DBObject) {
dbRefToRelation((DBObject) obj);
}
}
}
UserDaoImpl2Mongo.java 文件源码
项目:FindMe_Server
阅读 25
收藏 0
点赞 0
评论 0
@Override
public List<Map<String, Object>> queryUserFriendList(String userId) {
// TODO Auto-generated method stub
BasicDBObject column = new BasicDBObject("userFriend", 1);
Map<String, Object> user = super.findOneById(userId, column);
BasicDBList friendList = (BasicDBList) user.get("userFriend");
List<Map<String, Object>> friends = new ArrayList<Map<String, Object>>();
for (int i = 0; i < friendList.size(); i++) {
DBRef friend = (DBRef) friendList.get(i);
DBObject friendObj = friend.fetch();
Map<String, Object> fre = new HashMap<String, Object>();
fre.put("_id", friendObj.get("_id"));
fre.put("userNickName", friendObj.get("userNickName"));
fre.put("userPhoto", friendObj.get("userPhoto"));
fre.put("userSignature", friendObj.get("userSignature"));
// System.out.println(fre);
friends.add(fre);
}
return friends;
}
UserDaoImpl2Mongo.java 文件源码
项目:FindMe_Server
阅读 26
收藏 0
点赞 0
评论 0
@Override
public boolean insertUserFans(String girlId, String boyId) {
// TODO Auto-generated method stub
DB db = MongoDBUtil.getDB();
BasicDBObject girl = new BasicDBObject();
girl.put("_id", girlId);
DBRef boyDbRef = new DBRef(db, "user", boyId);
BasicDBObject update = new BasicDBObject();
update.put("user", boyDbRef);
update.put("isPass", 0);
DBCollection users = db.getCollection(userCollection);
WriteResult res = users.update(girl, new BasicDBObject("$addToSet",
new BasicDBObject("userFans", update)), false, true);
return res.getN() > 0 ? true : false;
}
UserDaoImpl2Mongo.java 文件源码
项目:FindMe_Server
阅读 22
收藏 0
点赞 0
评论 0
@Override
public boolean insertUserBoyMatch(String boyId, String girlId) {
// TODO Auto-generated method stub
DB db = MongoDBUtil.getDB();
BasicDBObject boy = new BasicDBObject();
boy.put("_id", boyId);
DBRef girlDbRef = new DBRef(db, "user", girlId);
BasicDBObject update = new BasicDBObject();
update.put("user", girlDbRef);
update.put("isPass", 0);
DBCollection users = db.getCollection(userCollection);
WriteResult res = users.update(boy, new BasicDBObject("$addToSet",
new BasicDBObject("userMatch", update)), false, true);
return res.getN() > 0 ? true : false;
}
TestMain.java 文件源码
项目:FindMe_Server
阅读 30
收藏 0
点赞 0
评论 0
public void query() {
DB db = MongoDBUtil.getDB();
DBCollection connection = db.getCollection("user");
BasicDBObject user = new BasicDBObject();
// ReadPreference preference = ReadPreference.valueOf("$ref");
user.put("_id", "2014062710281828268456656");
DBObject obj = connection.findOne(user);
// DBRefBase base = new DBRefBase(db, "post",
// "2014062509521826635294167");
// DBObject a = base.fetch();
DBRef b = (DBRef) obj.get("post");
DBObject a = b.fetch();
// Object b = obj.get("post");
System.out.println(a);
System.out.println(obj.toMap());
}
RootMongoResource.java 文件源码
项目:liveoak
阅读 22
收藏 0
点赞 0
评论 0
@Override
protected DBRef getDBRef(String uri) throws ResourceProcessingException {
if (uri != null) {
String rootURI = this.uri().toString();
if (uri.startsWith(rootURI + "/")) {
String resourcePath = uri.substring(rootURI.length());
String[] paths = resourcePath.split("/");
if (paths.length != 3) {
throw new ResourceProcessingException("$DBRefs must be in the format /rootContextPath/collectionName/resourceID");
} else {
String collectionName = paths[1];
String resourceID = paths[2];
return new DBRef(this.db(), collectionName, resourceID);
}
} else {
throw new ResourceProcessingException("$DBRefs are only supported under the same context root. URL specified ('" + uri + "')should start with " + rootURI
+ ".");
}
} else {
throw new ResourceProcessingException("$DBRefs must be URL, they cannot be null.");
}
}
MongoBaseObjectResource.java 文件源码
项目:liveoak
阅读 27
收藏 0
点赞 0
评论 0
@Override
public Resource member(RequestContext ctx, String id) throws Exception {
Object object = getDBObject().get(id);
if (object == null) {
return null;
}
if (object != null) {
if (object instanceof BasicDBObject || object instanceof BasicDBList) {
return null;
} else if (object instanceof DBRef) {
// TODO In case of exception make sure it results in InvalidRequest
//try {
return getResource((DBRef) object, ctx.returnFields().child(id).isEmpty());
//} catch (ResourceProcessingException e) {
// responder.invalidRequest(e.getMessage());
//}
} else {
throw new RuntimeException("ERROR: Object type (" + object.getClass() + ") not recognized");
}
}
return null;
}
MongoObjectResource.java 文件源码
项目:liveoak
阅读 22
收藏 0
点赞 0
评论 0
@Override
public Resource member(RequestContext ctx, String id) throws Exception {
Object object = getDBObject().get(id);
if (object != null) {
if (object instanceof BasicDBObject || object instanceof BasicDBList) {
return null;
} else if (object instanceof DBRef) {
//try {
// TODO throw exception that will end up as InvalidRequest
return getResource((DBRef) object, ctx.returnFields().child(id).isEmpty());
//} catch (ResourceProcessingException e) {
// responder.invalidRequest(e.getMessage());
//}
} else {
// TODO throw exception that will end up as InternalError
//responder.internalError("ERROR: Object type (" + object.getClass() + ") not recognized");
throw new RuntimeException("ERROR: Object type (" + object.getClass() + ") not recognized");
}
}
return null;
}
MongoObjectResource.java 文件源码
项目:liveoak
阅读 24
收藏 0
点赞 0
评论 0
@Override
public Map<String, ?> properties(RequestContext ctx) throws Exception {
// TODO: only read properties specified in the return fields and not everything
Map<String, Object> result = new HashMap<>();
Set<String> keys = getDBObject().keySet();
for (String key : keys) {
if (!key.equals(MONGO_ID_FIELD) && !key.equals(LiveOak.ID)) {
Object value = getDBObject().get(key);
if (value instanceof BasicDBObject) {
value = new MongoEmbeddedObjectResource(this, (DBObject) value);
} else if (value instanceof BasicDBList) {
value = getResourceCollection(value);
} else if (value instanceof DBRef) {
value = getResource((DBRef) value, ctx.returnFields().child(key).isEmpty());
}
if (supportedObject(value)) {
result.put(key, value);
} else {
log.warn("Unsupported Property type " + value.getClass() + " cannot encode.");
}
}
}
return result;
}
MongoObjectResource.java 文件源码
项目:liveoak
阅读 24
收藏 0
点赞 0
评论 0
protected Object getResourceCollection(Object object) throws Exception {
if (object instanceof BasicDBObject) {
return new MongoEmbeddedObjectResource(this, (DBObject) object);
} else if (object instanceof BasicDBList) {
BasicDBList dbList = ((BasicDBList) object);
ArrayList list = new ArrayList();
for (int i = 0; i < dbList.size(); i++) {
Object child = dbList.get(i);
list.add(getResourceCollection(child));
}
return list;
} else if (object instanceof DBRef) {
return getResource((DBRef) object, true);
} else {
return object;
}
}
MongoStatisticsDao.java 文件源码
项目:enviroCar-server
阅读 27
收藏 0
点赞 0
评论 0
private DBObject matches(StatisticsFilter request) {
BasicDBObjectBuilder b = new BasicDBObjectBuilder();
BasicDBObjectBuilder match = b.push(Ops.MATCH);
if (request.hasTrack()) {
DBRef track = mongoDB.ref(request.getTrack());
match.add(MongoMeasurement.TRACK, track);
}
if (request.hasUser()) {
DBRef user = mongoDB.ref(request.getUser());
match.add(MongoMeasurement.USER, user);
}
if (request.hasSensor()) {
MongoSensor sensor = (MongoSensor) request.getSensor();
match.add(SENSOR_ID_PATH, sensor.getId());
}
return b.get();
}
Mapper.java 文件源码
项目:morphia
阅读 25
收藏 0
点赞 0
评论 0
/**
* Converts a Key to a DBRef
*
* @param key the Key to convert
* @return the DBRef
*/
public DBRef keyToDBRef(final Key key) {
if (key == null) {
return null;
}
if (key.getType() == null && key.getCollection() == null) {
throw new IllegalStateException("How can it be missing both?");
}
if (key.getCollection() == null) {
key.setCollection(getCollectionName(key.getType()));
}
Object id = key.getId();
if (isMapped(id.getClass())) {
id = toMongoObject(id, true);
}
return new DBRef(key.getCollection(), id);
}
KeyConverter.java 文件源码
项目:morphia
阅读 23
收藏 0
点赞 0
评论 0
@Override
public Object decode(final Class targetClass, final Object o, final MappedField optionalExtraInfo) {
if (o == null) {
return null;
}
if (!(o instanceof DBRef)) {
throw new ConverterException(String.format("cannot convert %s to Key because it isn't a DBRef", o.toString()));
}
DBRef ref = (DBRef) o;
MappedField actualType = getActualType(optionalExtraInfo);
final Class<?> keyType = actualType != null
? actualType.getConcreteType()
: getMapper().getClassFromCollection(ref.getCollectionName());
final Key<?> key = new Key<Object>(keyType, ref.getCollectionName(), ref.getId());
return key;
}
MergeDetails.java 文件源码
项目:teiid
阅读 19
收藏 0
点赞 0
评论 0
public DBRef getDBRef(DB db, boolean push) {
if (this.id != null) {
if (this.idReference != null) {
return new DBRef(db, push?this.parentTable:this.embeddedTable, new DBRef(db, this.idReference, this.id.getValue()));
}
return new DBRef(db, push?this.parentTable:this.embeddedTable, this.id.getValue());
}
return null;
}
MongoDocument.java 文件源码
项目:teiid
阅读 22
收藏 0
点赞 0
评论 0
public DBObject getEmbeddedDocument(DB mongoDB, String docName) {
for (MergeDetails ref:this.embeddedKeys) {
if (ref.getName().equals(docName)) {
DBRef dbRef = ref.getDBRef(mongoDB, false);
if (dbRef != null) {
return mongoDB.getCollection(dbRef.getRef()).findOne(new BasicDBObject("_id", dbRef.getId())); //$NON-NLS-1$
}
}
}
return null;
}
ExpressionEvaluator.java 文件源码
项目:teiid
阅读 28
收藏 0
点赞 0
评论 0
private Object getRowValue(Expression obj) throws TranslatorException {
if (!(obj instanceof ColumnReference)) {
throw new TranslatorException(MongoDBPlugin.Util.gs(MongoDBPlugin.Event.TEIID18017));
}
ColumnReference column = (ColumnReference)obj;
Object value = null;
if (MongoDBSelectVisitor.isPartOfPrimaryKey(column.getTable().getMetadataObject(), column.getName())) {
// this is true one to many case
value = this.row.get("_id"); //$NON-NLS-1$
if (value == null) {
value = getValueFromRowInfo(column, value);
}
}
if (value == null && MongoDBSelectVisitor.isPartOfForeignKey(column.getTable().getMetadataObject(), column.getName())) {
value = getValueFromRowInfo(column, value);
}
if (value == null) {
value = this.row.get(column.getName());
}
if (value instanceof DBRef) {
value = ((DBRef)value).getId();
}
if (value instanceof DBObject) {
value = ((DBObject) value).get(column.getName());
}
return this.executionFactory.retrieveValue(value, column.getType(), this.mongoDB, column.getName(), column.getName());
}
ReferenceUtil.java 文件源码
项目:bugu-mongo
阅读 23
收藏 0
点赞 0
评论 0
public static String fromDbReference(Ref ref, Object value){
String result;
if(ref.reduced()){
result = value.toString();
}else{
DBRef dbRef = (DBRef)value;
result = dbRef.getId().toString();
}
return result;
}
ReferenceUtil.java 文件源码
项目:bugu-mongo
阅读 28
收藏 0
点赞 0
评论 0
public static String fromDbReference(RefList refList, Object value){
String result;
if(refList.reduced()){
result = value.toString();
}else{
DBRef dbRef = (DBRef)value;
result = dbRef.getId().toString();
}
return result;
}
EntityDataController.java 文件源码
项目:restfiddle
阅读 27
收藏 0
点赞 0
评论 0
private void relationToDBRef(DBObject dbObject, String projectId) {
for (String key : dbObject.keySet()) {
Object obj = dbObject.get(key);
if (obj instanceof DBObject) {
DBObject doc = (DBObject) obj;
if (doc.containsField("_rel")) {
DBObject relation = (DBObject) doc.get("_rel");
dbObject.put(key, new DBRef(projectId + "_" + (String) relation.get("entity"), new ObjectId((String) relation.get("_id"))));
} else {
relationToDBRef(doc, projectId);
}
}
}
}
EntitySessionController.java 文件源码
项目:restfiddle
阅读 34
收藏 0
点赞 0
评论 0
@RequestMapping(value = "/api/{projectId}/entities/login", method = RequestMethod.POST, headers = "Accept=application/json", consumes = "application/json")
public @ResponseBody
String createEntityData(@PathVariable("projectId") String projectId,
@RequestBody Object userDTO) {
DBObject data;
if (!(userDTO instanceof Map)) {
return null;
}
JSONObject response = new JSONObject();
Map map = (Map) userDTO;
JSONObject jsonObj = new JSONObject(map);
try {
data = authService.authenticate(jsonObj, projectId);
} catch (Exception e) {
response.put("msg", e.getMessage());
return response.toString(4);
}
DBCollection userCollection = mongoTemplate.getCollection(projectId + "_User");
DBObject user = userCollection.findOne(((DBRef)(data.get("user"))).getId());
user.removeField("password");
data.put("user", user);
dbRefToRelation(data);
data.put("authToken", data.get("_id"));
data.removeField("_id");
data.removeField("expireAt");
String json = data.toString();
// Indentation
response = new JSONObject(json);
return response.toString(4);
}
EntityAuthService.java 文件源码
项目:restfiddle
阅读 23
收藏 0
点赞 0
评论 0
public DBObject authenticate(JSONObject userDTO, String projectId) throws Exception{
DBCollection dbCollection = mongoTemplate.getCollection(projectId+"_User");
BasicDBObject query = new BasicDBObject();
query.append("username", userDTO.get("username"));
DBObject user = dbCollection.findOne(query);
if(user == null){
throw new Exception("User not found");
}
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
BasicDBObject auth;
if(encoder.matches((String)userDTO.get("password"),(String)user.get("password"))){
auth = new BasicDBObject();
auth.append("user", new DBRef(projectId+"_User",user.get( "_id" ))).append("expireAt", new Date(System.currentTimeMillis() + 3600 * 1000));
auth.put("projectId", projectId);
DBCollection dbCollectionAuth = mongoTemplate.getCollection(ENTITY_AUTH);
dbCollectionAuth.insert(auth);
}else{
throw new Exception("Invalid password");
}
return auth;
}
UserDaoImpl2Mongo.java 文件源码
项目:FindMe_Server
阅读 24
收藏 0
点赞 0
评论 0
@Override
public boolean addFriend(String userId, String likeUserId) {
// TODO Auto-generated method stub
DB db = MongoDBUtil.getDB();
DBCollection users = db.getCollection(userCollection);
BasicDBObject user = new BasicDBObject("_id", userId);
DBRef friendREF = new DBRef(db, userCollection, likeUserId);
BasicDBObject friend = new BasicDBObject("userFriend", friendREF);
boolean flag = false;
// userId用户添加likeUserId用户好友
WriteResult result = users.update(user, new BasicDBObject("$push",
friend), false, true);
int i = result.getN();
if (i > 0) {
BasicDBObject user2 = new BasicDBObject("_id", likeUserId);
DBRef friendREF2 = new DBRef(db, userCollection, userId);
BasicDBObject friend2 = new BasicDBObject("userFriend", friendREF2);
// likeUserId用户添加userId用户好友
result = users.update(user2, new BasicDBObject("$push", friend2),
false, true);
i = result.getN();
if (i > 0)
flag = true;
}
return flag;
}
UserDaoImpl2Mongo.java 文件源码
项目:FindMe_Server
阅读 23
收藏 0
点赞 0
评论 0
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> queryPushUser(String sort) {
// TODO Auto-generated method stub
DB db = MongoDBUtil.getDB();
DBCollection users = db.getCollection("puser");
BasicDBObject user = new BasicDBObject("sort", Integer.parseInt(sort));
DBObject u = users.findOne(user);
DBRef puser = (DBRef) u.get("user");
BasicDBObject obj = (BasicDBObject) puser.fetch();
obj.remove("userLoginLog");
obj.remove("userAuthType");
obj.remove("userEquipment");
obj.remove("userLastLoginIpAddress");
obj.remove("userLastLoginTime");
if (obj != null && obj.isEmpty() == false) {
return obj.toMap();
}
return null;
}
UserDaoImpl2Mongo.java 文件源码
项目:FindMe_Server
阅读 36
收藏 0
点赞 0
评论 0
@SuppressWarnings("unchecked")
@Override
public List<Map<String, Object>> queryUserFansList(String userId) {
// TODO Auto-generated method stub
Map<String, Object> user = super.findOneById(userId, null);
List<Map<String, Object>> fans = new ArrayList<Map<String, Object>>();
if (user != null && user.isEmpty() == false) {
BasicDBList userFans = (BasicDBList) user.get("userFans");
for (int i = 0; i < userFans.size(); i++) {
BasicDBObject _obj = (BasicDBObject) userFans.get(i);
DBRef dbRef = (DBRef) _obj.get("user");
BasicDBObject obj = (BasicDBObject) dbRef.fetch();
obj.remove("userEquipment");
obj.remove("userOpenId");
obj.remove("userLastLoginIpAddress");
obj.remove("userLoginLog");
obj.remove("userMatch");
obj.remove("userFans");
obj.remove("userLike");
obj.remove("userFriend");
fans.add(obj.toMap());
}
}
return fans;
}
NewsDaoImpl2Mongo.java 文件源码
项目:FindMe_Server
阅读 25
收藏 0
点赞 0
评论 0
@Override
public void createPostNews(Map<String, Object> postNews,
String collectionName, String postId) {
DB db = MongoDBUtil.getDB();
DBRef dbRef = new DBRef(db, collectionName, postId);
postNews.put("post", dbRef);
DBCollection collection = db.getCollection(newsCollection);
BasicDBObject news = new BasicDBObject(postNews);
collection.insert(news);
// super.insertByJson(postNews);
}
NewsDaoImpl2Mongo.java 文件源码
项目:FindMe_Server
阅读 26
收藏 0
点赞 0
评论 0
@Override
public void createPostNews(String postId, String userId) {
// TODO Auto-generated method stub
DB db = MongoDBUtil.getDB();
List<Map<String, Object>> users = new ArrayList<Map<String, Object>>();
Map<String, Object> user = new HashMap<String, Object>();
DBRef userDbRef = new DBRef(db, "user", userId);
user.put("user", userDbRef);
user.put("isRead", "1");// 是否已经阅读 1:已阅读 0:未阅读
user.put("isDel", 1);// 是否删除 1:删除 0:未删除
user.put("_id", userId);
users.add(user);
Map<String, Object> postNews = new HashMap<String, Object>();
postNews.put("_id", super.methodUtil.getUUID());
postNews.put("users", users);
postNews.put("updateTime", methodUtil.formatDate(new Date(), null, 0));
// DBRef dbRef = new DBRef(db, "post", "2014062509521826635294167");
DBRef dbRef = new DBRef(db, "post", postId);
postNews.put("post", dbRef);
DBCollection collection = db.getCollection(newsCollection);
BasicDBObject news = new BasicDBObject(postNews);
collection.insert(news);
}
NewsDaoImpl2Mongo.java 文件源码
项目:FindMe_Server
阅读 24
收藏 0
点赞 0
评论 0
@SuppressWarnings("unchecked")
@Override
public List<Map<String, Object>> queryNewsListByRefresh(String userId) {
List<Map<String, Object>> postList = new ArrayList<Map<String, Object>>();
BasicDBObject query = new BasicDBObject();
query.put("users._id", userId);
query.put("users.isDel", 0);
BasicDBObject sort = new BasicDBObject();
sort.put("updateTime", -1);
List<Map<String, Object>> m = super.findByParams(query, sort,
CommonVariables.PAGE_SIZE, null);
for (Map<String, Object> map : m) {
DBRef dbRef = (DBRef) map.get("post");
BasicDBObject post = (BasicDBObject) dbRef.fetch();
post.remove("postMessage");
Map<String, Object> postMap = post.toMap();
postMap.put("updateTime", map.get("updateTime"));
BasicDBList users = (BasicDBList) map.get("users");
for (int i = 0; i < users.size(); i++) {
BasicDBObject user = (BasicDBObject) users.get(i);
String _id = user.getString("_id");
if (_id.equals(userId)) {
postMap.put("isRead", user.getString("isRead"));
break;
}
}
postList.add(postMap);
}
return postList;
}
NewsDaoImpl2Mongo.java 文件源码
项目:FindMe_Server
阅读 20
收藏 0
点赞 0
评论 0
@SuppressWarnings("unchecked")
@Override
public List<Map<String, Object>> queryNewsListByLoadMore(String userId,
String newsId) {
List<Map<String, Object>> postList = new ArrayList<Map<String, Object>>();
BasicDBObject query = new BasicDBObject();
query.put("users._id", userId);
query.put("users.isDel", 0);
query.put("_id", new BasicDBObject("$lt",newsId));
BasicDBObject sort = new BasicDBObject();
sort.put("updateTime", -1);
List<Map<String, Object>> m = super.findByParams(query, sort,
CommonVariables.PAGE_SIZE, null);
for (Map<String, Object> map : m) {
DBRef dbRef = (DBRef) map.get("post");
BasicDBObject post = (BasicDBObject) dbRef.fetch();
post.remove("postMessage");
Map<String, Object> postMap = post.toMap();
postMap.put("updateTime", map.get("updateTime"));
BasicDBList users = (BasicDBList) map.get("users");
for (int i = 0; i < users.size(); i++) {
BasicDBObject user = (BasicDBObject) users.get(i);
String _id = user.getString("_id");
if (_id.equals(userId)) {
postMap.put("isRead", user.getString("isRead"));
break;
}
}
postList.add(postMap);
}
return postList;
}
TestMain.java 文件源码
项目:FindMe_Server
阅读 31
收藏 0
点赞 0
评论 0
public void insertPushFriend() {
DB db = MongoDBUtil.getDB();
DBCollection connection = db.getCollection("puser");
BasicDBObject puser = new BasicDBObject();
puser.put("_id", methodUtil.getUUID());
DBRef dbRef = new DBRef(db, "user", "2014062909381246721829541");
puser.put("user", dbRef);
puser.put("sort", 3);
connection.insert(puser);
}