sql >> Database teknologi >  >> NoSQL >> MongoDB

MongoDB bedste praksis for referencer

Du kan bruge MongoDBRef objekt i stedet for brugerobjekt.

public class Post : Entity
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Summary { get; set; }
    public DateTime Added { get; set; }
    public MongoDBRef Owner { get; set; }
}    

Så kan du:

var mongo = new Mongo(config.BuildConfiguration());
mongo.Connect();        
var DB = mongo.GetDatabase(_dataBaseName)

var post = new Post();
post.Owner = new MongoDBRef("User", userId); // First parameter is a mongoDB collection name and second is object id
// To fetch object referenced by DBRef you should do following
var owner = DB.FollowReference<User>(post.Owner);


  1. Sådan udelukker du nogle felter fra dokumentet

  2. deling af IO-objekt i flere node-forekomster

  3. Opret et 2dsphere Geospatial Index for sfæriske forespørgsler i MongoDB

  4. Redis vs. Memcached – 2021 Sammenligning