在spring-data-mongodb中检索map mongo集合

时间:2018-10-17 09:44:17

标签: java spring-data-mongodb

我正在使用spring-data-mongo检索我的通知集合中的此类对象:

_id:ObjectId("123123123123")
1510067387875:Object
    date:"1510067387875"
    title:"Sample title"
    text:"Sample Text"
_id:ObjectId("123223123123")
1110067387875:Object
    date:"1110067387875"
    title:"Sample title"
    text:"Sample Text"

所以我已经定义了我的存储库类:NotificationRepository

@Repository
@Transactional("mongoTransactionManager")
@PersistenceContext(name = "mongodbEntityManager")
public interface NotificationRepository extends MongoRepository<NotificationMap, ObjectId> {
}

和NotificationMap:

@Document(collection = "notifications")
public class NotificationMap {

@Id
private ObjectId _id;

private Map<String, Notification> map;

/**
 * @return the map
 */
public Map<String, Notification> getMap() {
    return map;
}

/**
 * @param map the map to set
 */
public void setMap(Map<String, Notification> map) {
    this.map = map;
}

public ObjectId get_id() {
    return _id;
}

/**
 * @param _id the _id to set
 */
public void set_id(ObjectId _id) {
    this._id = _id;
}

}

最后是Notification类:

public class Notification {
    private long date;
    private String title;
    private String text;
    private String dateString;

但是返回空对象。我应该如何访问信息

1 个答案:

答案 0 :(得分:0)

ConvertJson.jsonToString(json: menu as AnyObject) class func jsonToString(json: AnyObject) -> String{ var item : String! do { let data1 = try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted) // first of all convert json to the data let convertedString = String(data: data1, encoding: String.Encoding.utf8) // the data will be converted to the string item = convertedString! } catch let myJSONError { print(myJSONError) } return item } 似乎是不必要的。只需使用:

NotificationMap