如何正确保存键值对在猫鼬中

时间:2018-12-13 18:48:15

标签: mongodb mongoose

我想将此示例数据保存在我的mongodb中。但是我不知道如何实现这一目标,也不知道如何声明要保存的对象的类型。城市应该有一个mongoose.Schema.Types.ObjectId和一个ref: 'Country'

以这种格式保存数据的脚本看起来如何,模型看起来如何?

城市

{
    name: Paris,
    number: 001,
    country: 5c1292439da9b851a833cbba
}
{
    name: Berlin,
    number: 002,
    country: 5c1292439da9b851a833aabc
}
...

国家/地区

{
    id: 5c1292439da9b851a833cbba
    name: France
    number: 001
}
{
    id: 5c1292439da9b851a833aabc
    name: Germany
    number: 002
}

2 个答案:

答案 0 :(得分:1)

    public class Schema: GraphQL.Types.Schema
    {
        public Schema(IDependencyResolver resolver): base(resolver)
        {
            Query = resolver.Resolve<RootQuery>();  
            Mutation = null;
        }
    }

    public class RootQuery: ObjectGraphType
    {
        public RootQuery(IDependencyResolver resolver)
        {
            Name = "Query";

            Field<MySubQuery>(
                name: "subQuery",
                resolve: ctx => resolver.Resolve<MySubQuery>());
        }
    }


    public class MySubQuery: ObjectGraphType
    {
        public MySubQuery()
        {
            Name = "TempSubQuery";

            Field<StringGraphType>("SubQueryName", resolve: ctx => "Some string value");

            Field<MySubObjectGraphType>(
                name: "subObjectGraph",
                resolve: ctx => FetchFromRepo());
        }


        //Repo access would go here, but just new-ing the object for now.
        private SubObject FetchFromRepo()
        {
            return new SubObject() { Name = "some sub object" };
        }
    }


    public class SubObject
    {
        public string Name { get; set; }
    }

    public class MySubObjectGraphType: ObjectGraphType<SubObject>
    {
        public MySubObjectGraphType()
        {
            Name = "MySubObject";
            Description = "An object with leaf nodes";

            Field(l => l.Name);
        }
    }

答案 1 :(得分:0)

如果您希望在Nodejs Mozilla中创建模式,请在此主题上写一个好文章。

您的样子:

clear all